Question

In: Computer Science

Write a short bash program that aligns with the below functionality. $ sh ~/cs210_list.sh Error: missing...

Write a short bash program that aligns with the below functionality.

$ sh ~/cs210_list.sh

  Error: missing argument

$ echo $?

1

$ sh ~/cs210_list.sh /non-exist

  Error: ‘/non-exist’ is an invalid directory

$ echo $?

1

$ sh ~/cs210_list.sh /etc

File: aavark

File: aabbb

   Size: 16

File: aaacccc

File: aaaaddd

   Size: 1585

File: aliases~

   Size: 1686

File: aliases.db

   Size: 12288

...


Notice:

  • It error checks that the first argument exists and is a directory.
  • It generates an error message and returns a failure code in error situations.
  • It only outputs a file size when the item is a file (and not the name of a directory).

Grading Criteria:

In my check, I will ask you to do the following checks:

  • If the script has no arguments, it should:
    • Print the following: "Error: missing argument". (5pts)
    • Generate error code 1; Check this by doing the command "echo $?" right afterwards. (5pts)
  • If the script is given an argument that points to an invalid directory (example: the /non-exist directory), then your code should:
    • Print the following: " Error: ‘<insert argument here>’ is an invalid directory". Don't forget to print out the provided argument. (5pts)
    • Generate error code 1; Check this by doing the command "echo $?" right afterwards. (5pts)
  • Otherwise, if the directory is normal, it should run just like the question titled "Exercise: a simple loop".
    • In short, it should print the name (5pts) and size (5pts) of each file in the directory, in the exact format like above and the prior question.

Copy and paste your program below

Solutions

Expert Solution

i) # Shell script for missing argument

 if [ $# -eq 0 ]

then

echo " Error: Missing argument"

else

echo " There are $# arguments and they are:  $* "

fi

*************************************************

# Script to pass an arument through command line and check whether it is a valid directory or not

if [ $# -eq 0 ] # $# always holds the number of arguments passed, this checks for arguments absence

then

echo " No arguments are passed"

elif [ $# -eq 1 -a -d $1 ] # This checks for 1 argument and must be a valid directory.

then

echo "It is a valid directory"

else

echo "It is not a directory"

fi

**************************************************

iii)

Note : I am considering the argument passed is a directory and it will print only the ordinary file name along with it size.

# Script to display the name of a file along with its size

if [ $# -eq 0 ]

then

echo " Pass a valid directory as an argument .........."

elif [ $# -eq 1 -a -d $1 ]

then

ls -lR $1 | tr -s ' ' | cut -d ' ' -f 9 > a

ls -lR $1 | tr -s ' ' | cut -d ' ' -f 5 > b

paste a b

fi

Note : $? is a special parameter used to check exit status of a command ...


Related Solutions

Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a short bash script that takes in two arguments from a user, asks the user...
Write a short bash script that takes in two arguments from a user, asks the user whether they would like to add, subtract, multiply, or divide. Each of these operations must be a function that returns data.
In MPLAB write and compile (using the simulator) an assembly language program with the following functionality:...
In MPLAB write and compile (using the simulator) an assembly language program with the following functionality: Configures pin RA2 of the PIC24to be an output to control an attached LED. Configures pin RB13 of the PIC24 to be an input to read the value on an attached switch (this switch will connect to ground when pressed). Configures pin RB13 to use the internal pull-up resistor. After configuration, the LED will be "off" when the push-button is pressed, and "on" when...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
write a bash shell program named L1 as follows . L1 expects exactly 2 command line...
write a bash shell program named L1 as follows . L1 expects exactly 2 command line arguments. expects $1 to contain only digits. expects $2 to have exactly 4 characters (can be any character). if NOT exactly 2 command line arguments, echo "need 2 args " and exit. otherwise, if $1 and/or $2 have incorrect value(s) (as above), echo "bad argX", where X is the first incorrect argument, and exit. otherwise, echo "good ". you can use echo,grep, pipe to...
Related with the below statements, write the missing words: A variation of linked list, named as...
Related with the below statements, write the missing words: A variation of linked list, named as Doubly Linked List contains a link element called _______,_______. The heap sort has an average-case complexity of _________. The algorithms like merge sort, quick sort and binary search are based on __________ and _____________ algorithm. In stack terminology, the insertion operation is defined to be ___________. The asymptotic notation O(1) defines the complexity of type ___________. Looking for the next cell in the array,...
Why am I getting error below? sh-3.2# ./week4prog1[name].scr ./week4prog1[name].scr: line 2: count: command not found start...
Why am I getting error below? sh-3.2# ./week4prog1[name].scr ./week4prog1[name].scr: line 2: count: command not found start of the program ./week4prog1[name].scr: line 4: [: -le: unary operator expected end of the program Below is my vim script I am running. I can't see the error, please help: #!/bin/bash count =1 echo "start of the program" while [ $count -le 10 ] do echo "Loop #$count" sleep 10 count=$[ count + 1] done echo "end of the program"
Write a short program that asks the user for a sentence and prints the result of...
Write a short program that asks the user for a sentence and prints the result of removing all of the spaces. Do NOT use a built-in method to remove the spaces. You should programmatically loop through the sentence (one letter at a time) to remove the spaces.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT