Question

In this exercise, you will create a script called file_ops.sh that uses the various file operators...

  • In this exercise, you will create a script called file_ops.sh that uses the various file operators
  • The file_ops.sh script will
  • Use one command line argument
  • Use four IF THEN statement to compare the first command line argument
  • Determine which file operators to use that produces the following output when the script run with the arguments shown

WARNING: In the IF THEN statements, to avoid syntax error messages, there must be a space before and after each bracket and also between each element within the brackets.

  • Watching the video about File Test Operators may be helpful
  • Look at the slide "File Operators"
  • The first IF THEN statement has the form

If [ operator $1 ]

then

      echo "<message>"

fi

  • Decide which operator to use that produces the message "… exists and is a file"

  • The second IF THEN statement has the form

If [ operator $1 ]

then

      echo "<message>"

fi

  • Decide which operator to use that produces the message "… exists"

  • The third IF THEN statement has the form

If [ operator $1 ]

then

      echo "<message>"

fi

  • Decide which operator to use that produces the message "… does not exist"

  • The fourth IF THEN statement has the form

if [ ! operator $1 ]

then

      echo "<message>"

fi

  • Decide which operator to use that produces the message "… exists and is a directory"
  • Type the following commands. NOTE: You do not need to use 'gedit' to create your shell script. You can use 'vi' or 'nano' if you prefer
  • Ignore anything highlighted in red as they are just comments

'clear' // Clear the screen

'cd ~/scripting_lab' // Change to the lab directory using tilde

'gedit file_ops.sh' // create the script using the editor of choice

'cat –n file_ops.sh' // show the script contents

// You will run file_ops.sh four times without changes to the script

'sh file_ops.sh /etc/passwd' // file exists

'sh file_ops.sh /etc/passwdd' // file does not exist

'sh file_ops.sh /etc' // directory

'sh file_ops.sh /etcc' // directory does not exist

'date'

  • Using Snipping Tool, take a screenshot
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

arg=$1

if [ -f $arg ]
then
   echo "$arg exists and is a file"
fi
if [ -e $arg ]
then
   echo "$arg exists"
fi
if [ ! -e $arg ]
then
   echo "$arg does not exist"
fi
if [ -d $arg ]
then
   echo "$arg exists and is a directory"
fi


Screenshot: ( for reference )

Output:

Note: If you have any doubts please comment.

It will be great help If you like.

Add a comment
Know the answer?
Add Answer to:
In this exercise, you will create a script called file_ops.sh that uses the various file operators...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this exercise, you will create a script called if_then_else.sh that uses an IF THEN ELSE...

    In this exercise, you will create a script called if_then_else.sh that uses an IF THEN ELSE statement. The if_then_else.sh script will Use one command line argument, which will be a number between 1 and 10 Assign the value of the first argument to a local variable called NUM. HINT: Look at line 5 in the example screenshot for arguments.sh Use one IF THEN ELSE statement to compare the value of NUM If the number is less than 5, display the...

  • UNIX shell script that will create a backup of the file specified as an argument. This...

    UNIX shell script that will create a backup of the file specified as an argument. This backup file will be saved in ~/backups/ with -date appended to the original file name. the date shall be in the form yymmdd:HHmm. Your script should check to make sure the ~/backups/ directory exists before copying files, creating the directory if necessary. Ensure your script will properly accept a file argument with path information and strip any path information prior to copying the file....

  • LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command...

    LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command line arguments and does NOT use the read command. I will give you no credit if your bash script includes a read command. Also, you need to have at least one for..in loop in your code even if there may be other ways to do this problem. The script will go through a list of entries in the current directory (do not process subdirectories):...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • Create a Windows batch script. The batch script will contain the following: Ask the user interactively:...

    Create a Windows batch script. The batch script will contain the following: Ask the user interactively: Boot drive letter of the computer Computer name Computer Model Check to see if the boot drive in #1 exists with the folder newfolder (#1 above) If yes cd BootDrive\newfolder If no, make the directory BootDrive\newfolder Check to see if casefile.txt exists: If yes goto the label DONE Tell the user that data already exists in the newfolder folder and exit the program Output...

  • Problem 1 Write a BASH script to create a user account from the Linux system on...

    Problem 1 Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters. First positional parameter is supposed to be a string with a user name (e.g., user_name) Second positional parameter is supposed to be a string with a user password (e.g., user_password) In your script: Check if two positional parameters were passed to your script when it was invoked If NOT, print an appropriate...

  • Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a....

    Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a. Write the following as script6.sh and change its permissions to count for num in d do if num eq 100 then count (count+1) done echo The number 10 was found $count times What is the output? Run the script as ./script6.sh 10 20 30 10 40 20 50 10 60 <enter Summarize what the script does r using the read command. The following script...

  • Open vi or pico and create a script called Information.   Add the contents from the following table to the script: (Hin...

    Open vi or pico and create a script called Information.   Add the contents from the following table to the script: (Hint: use echo to add blank lines to your output and to write out the headings). Headings: Utilities (command to execute) Today’s date is: date The current users logged on to the system are: who My current startup directory is: pwd Today’s identification information is: id The processes running on my system are: ps Add a header to your Information...

  • Write a bash script to protect a directory. Your script should notify a user of the...

    Write a bash script to protect a directory. Your script should notify a user of the following: -New file was created -Permission changes -Ownership changes -File size changes -Modification date changes Your script should have two operation modes, protect and check. Use command line flags, -p <directory> and -c <directory> to decide which mode to operate in. In protect mode, your script should create a file called .protect inside the directory you want to watch. You will use this file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT