Question
UnixLinux write a shell script. Reconsider this first assignment using functions: You are writing a shell script that has this synopsis: myss { -a |
Unix\Linux write a shell script.
Reconsider this first assignment using functions:
You are writing a shell script that has this synopsis:
myss { -a | -b } file
This means that myss requires two arguments. The first is an option, which must be either -a or -b. The second is a file. We will make the further restriction that the file argument must be a readable file.
Write code for this shell script to check its arguments. Your code should output an error if the arguments are invalid.
The solution we put together in class without using functions is available here:
#!/bin/bash
# This is going to take two arguments # Check the two arguments
# 1) Exactly two arguments # 2) The first option is either a -a or a -b # 3) File is there: the second argument is a valid file
if [ $# -ne 2 ]; then echo "There does not appear to be exactly two arguments" exit 1
elif [ $1 != "-a" -a $1 != "-b" ]; then echo "The first option has to be -a or a -b" exit 1
# Checks to see if second argument is a file elif ! [ -f $2 ]; then echo "The second argument has to be a file" exit 1
# Check to see if second argument is readable elif ! [ -r $2 ]; then echo "The second argument is not readable, it needs to be readable" exit 1
else echo "All parameters good" exit 0 fi
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started