Getting user input as a argument for a script

drew197 :

I am trying to make a bash script that gives the user options 1-5,with 5 being to exit. For option 2 I want the to be able to pass the file name as a argument for the file_size script but even when I catch the user input it still says the error message please enter a single file name as an argument. I am not sure why is doesn't pass it as a argument, Any help would be appreciated.

    #!/bin/bash

# usage: ./menu_script

exitvar=false 

while [ $exitvar = "false" ]  # keep looping until exit equal true
  do

echo -n "Enter a number 1 through 5, 5 to exit"
read number

case $number in
1) echo "Move empty files"
/home/student/move_empty;;

2) echo "Check file size"
echo -n "Enter a file name to check: "
read sourcefile

/home/student/file_size;;

3) echo "Option3";;

4) echo "File check rwx"
echo -n "Enter a file name to check: "
read  sourcefile

if [ -e $sourcefile ]  # does the source file exist? 
 then
   echo $sourcefile "exists!"
  fi     

# find out if file has write permission or not
[ -w $sourcefile ] && W="Write = yes" || W="Write = No"

# find out if file has excute permission or not
[ -x $sourcefile ] && X="Execute = yes" || X="Execute = No"

# find out if file has read permission or not
[ -r $sourcefile ] && R="Read = yes" || R="Read = No"

echo "$sourcefile permissions"
echo "$W"
echo "$R"
echo "$X"

;;

5) echo "exit"
   exitvar="true"
   ;;
 *) echo "Invalid number try again"
   ;;
 esac
done
Jetchisel :

At the option 2)

2) echo "Check file size"
  echo -n "Enter a file name to check: "
  read -r  sourcefile
  echo /home/student/file_size "$sourcefile";;
  • To see if the argument is being pass to /home/student/filesize

  • It would be nice and informative to show a menu with numbers alongside the option.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=6666&siteId=1