Logo 
Search:

Unix / Linux / Ubuntu Articles

Submit Article
Home » Articles » Unix / Linux / Ubuntu » Homework HelpRSS Feeds

Write a shell script, which will receive any number of filenames as arguments .The shell script should check whether such files already exist.

Posted By: Harvey Evans     Category: Unix / Linux / Ubuntu     Views: 8297

Write a shell script, which will receive any number of filenames as arguments .The shell script should check whether such files already exist. If they do, then it should be reported. If these files do not exist then check if a subdirectory called mydir exists in the current directory .If it doesn't exist then itshould be created and in it files supplied as arguments should get created .If mydir already exists then it should be reported along with the number of files that are currently present in myir.

Code for Write a shell script, which will receive any number of filenames as arguments .The shell script should check whether such files already exist. in Unix / Linux / Ubuntu

for i in $*
doif [ -f $i ]
        then
                echo "File $i is already exist."elseif [ -d mydir ]
                then
                        cnt=0
                        cd mydir
                        for j in `ls`
                        doif [ -f $j ]
                                then
                                        cnt=`expr $cnt + 1`
                                fi
                        done
                        echo "Mydir already exist...contains $cnt files"else
                        mkdir mydir
                        cd mydir
                        > $i
                fi
        fi
done

_______________________________________________________________________

OUTPUT:

[mca264@gama shellscript]$ sh 33.sh f2
File f2 is already exist.

[mca264@gama shellscript]$ ls
10.sh  14.sh  18.sh  2.sh   34.sh  38.sh  41.sh  7.sh  f2      temp1
11.sh  15.sh  19.sh  31.sh  35.sh  39.sh  4.sh   8.sh  SARITA  temp5
12.sh  16.sh  1.sh   32.sh  36.sh  3.sh   5.sh   9.sh  myfile  test555
13.sh  17.sh  20.sh  33.sh  37.sh  40.sh  6.sh   f1    parita

[mca264@gama shellscript]$ sh 33.sh f2
File f2 is already exist.
[mca264@gama sscript]$ ls
10.sh  14.sh  18.sh  2.sh   34.sh  38.sh  41.sh  7.sh  f2      temp1
11.sh  15.sh  19.sh  31.sh  35.sh  39.sh  4.sh   8.sh  SARITA  temp5
12.sh  16.sh  1.sh   32.sh  36.sh  3.sh   5.sh   9.sh  myfile  test555
13.sh  17.sh  20.sh  33.sh  37.sh  40.sh  6.sh   f1    parita

[mca264@gama shellscript]$ sh 33.sh file

[mca264@gama shellscript]$ ls
10.sh  14.sh  18.sh  2.sh   34.sh  38.sh  41.sh  7.sh  f2      myfile  test555
11.sh  15.sh  19.sh  31.sh  35.sh  39.sh  4.sh   8.sh  SARITA  parita
12.sh  16.sh  1.sh   32.sh  36.sh  3.sh   5.sh   9.sh  vd      temp1
13.sh  17.sh  20.sh  33.sh  37.sh  40.sh  6.sh   f1    mydir   temp5

[mca264@gama shellscript]$ cd mydir/

[mca264@gama mydir]$ ls
file
  
Share: 



Harvey Evans
Harvey Evans author of Write a shell script, which will receive any number of filenames as arguments .The shell script should check whether such files already exist. is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!