BASH Scripting

Todd Deshane deshantm at gmail.com
Sun Jun 26 21:13:46 UTC 2005


On 6/26/05, Matthew S-H <mathbymath at aol.com> wrote:
> 
> I hope I'm not becoming a bother. If I am, just let me know and I'll find 
> another list to post my BASH questions on.
> Anyway, I am still having a little trouble scripting. I don't quite 
> understand tests (when to use == or -eq, for example).
> 
> If any of you could look at my code, I'd appreciate it.
> 
> #!/bin/bash
> 
> #************************************************#
> # file_prompt.sh #
> # written by Matthew Strax-Haber #
> # Started June 19, 2005 #
> # Last Modified June 26, 2005 #
> # #
> # Lists files in a dir and asks #
> # the user which to select #
> #************************************************#
> 
> E_FILE_DOES_NOT_EXIST=85
> E_PERM_DENIED=87
> E_FILE_NOT_DIR=88
> E_NEED_ARGS=95
> E_INVALID_USER_INPUT=98
> work_dir=$1 # directory to work in
> prompt=${2-"Choose"} # prompt to show user
> full_pathQQQ=$3 # whether output should be full path
> #chosen_num # Num from prompt
> #chosen_file # Name of chosen file
> max_tries=3 # Max num of prompts if invalid file selected
> 
> 
> if test -z $work_dir -o -z "$prompt"
> then
> echo "$(basename $0): Missing arguments!" > /dev/stderr
> exit $E_NEED_ARGS
> elif test ! -e $work_dir
> then
> echo "$(basename $0): \"$work_dir\" does not exist!" > /dev/stderr
> exit $E_FILE_DOES_NOT_EXIST
> elif test ! -d $work_dir
> then
> echo "$(basename $0): \"$work_dir\" is not a directory!" > /dev/stderr
> exit $E_FILE_NOT_DIR
> elif test ! -x $work_dir
> then
> echo "$(basename $0): You do not have permission to list the files in 
> \"$work_dir\"!" > /dev/stderr
> exit $E_PERM_DENIED
> fi
> 
> if test -z $full_pathQQQ
> then
> full_pathQQQ=1
> fi
> 
> # --------------------------------------------------------- #
> # --------------------------------------------------------- #
> 
> command ls $work_dir | grep -n -v 'randtonotmatch' | column > /dev/stderr
> num_files=$(command ls $work_dir | grep -c -v 'randtonotmatch')
> digits=1
> while (( $num_files >= 10 ))
> do
> ((digits += 1))
> ((num_files /= 10))
> done
> tries=0
> while [ ! -e "$chosen_file" -o $tries != $max_tries ]
> do
> read -p "$prompt: " -n $digits chosen_num > /dev/stderr
> chosen_file=$(command ls $work_dir | grep -n -v 'randtonotmatch' | grep 
> ^$(($chosen_num)): | cut -d ":" -f 2-)
> if [ ! -e $chosen_file ]
> then
> ((tries += 1))
> echo -e "\nInvalid file selection! You have $((max_tries - tries)) left."
> fi
> done
> 
> if (( $max_tries = $tries ))
> then
> echo "$(basename $0): You have repeatedly chosen an invalid file! This 
> program will now quit." > /dev/stderr
> exit $E_INVALID_USER_INPUT
> # echo "$(basename $0): The chosen file does not exist!" > /dev/stderr
> fi
> echo " - $chosen_file" > /dev/stderr
> echo $chosen_file >&1
> 
> 
> 
> Also, what would be the best way to have a timeout period on prompts using 
> the command "read"? I want it to automatically choose what the user has 
> entered after 0.5 seconds. This is actually for a different script. Also, 
> if possible, it would be nice to have that timeout period restart if the 
> user types anything.
> 
> 
> Thank you very much to anyone who responds.
> ~Matt
> 
> PS: In case it would help, here is the version of this from before I tried 
> to implement "retries".
> 
> #!/bin/bash
> 
> #************************************************#
> # file_prompt.sh #
> # written by Matthew Strax-Haber #
> # Started June 19, 2005 #
> # Last Modified June 23, 2005 #
> # #
> # Lists files in a dir and asks #
> # the user which to select #
> #************************************************#
> 
> E_FILE_DOES_NOT_EXIST=85
> E_PERM_DENIED=87
> E_FILE_NOT_DIR=88
> E_NEED_ARGS=95
> work_dir=$1 # directory to work in
> prompt=${2-"Choose"} # prompt to show user
> full_pathQQQ=$3 # whether output should be full path
> #chosen_num # Num from prompt
> #chosen_file # Name of chosen file
> 
> 
> if test -z $work_dir -o -z "$prompt"
> then
> echo "$(basename $0): Missing arguments!" > /dev/stderr
> exit $E_NEED_ARGS
> elif test ! -e $work_dir
> then
> echo "$(basename $0): \"$work_dir\" does not exist!" > /dev/stderr
> exit $E_FILE_DOES_NOT_EXIST
> elif test ! -d $work_dir
> then
> echo "$(basename $0): \"$work_dir\" is not a directory!" > /dev/stderr
> exit $E_FILE_NOT_DIR
> elif test ! -x $work_dir
> then
> echo "$(basename $0): You do not have permission to list the files in 
> \"$work_dir\"!" > /dev/stderr
> exit $E_PERM_DENIED
> fi
> 
> if test -z $full_pathQQQ
> then
> full_pathQQQ=1
> fi
> 
> # --------------------------------------------------------- #
> # --------------------------------------------------------- #
> 
> if test -O $work_dir -a -d $work_dir
> then
> command ls $work_dir | grep -n -v 'randtonotmatch' | column > /dev/stderr
> num_files=$(command ls $work_dir | grep -c -v 'randtonotmatch')
> echo $num_files > /dev/stderr
> digits=1
> while (( $num_files >= 10 ))
> do
> ((digits += 1))
> ((num_files /= 10))
> done
> read -p "$prompt: " -n $digits chosen_num > /dev/stderr
> chosen_file=$(command ls $work_dir | grep -n -v 'randtonotmatch' | grep 
> ^$(($chosen_num)): | cut -d ":" -f 2-)
> 
> if [ ! -e $chosen_file ]
> then
> echo "$(basename $0): The chosen file does not exist!" > /dev/stderr
> fi
> 
> echo " - $chosen_file" > /dev/stderr
> echo $chosen_file >&1
> fi
> 
> 
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> http://lists.ubuntu.com/mailman/listinfo/ubuntu-users
> 
> 
> You are using the == wrong it seems.

When you want to compare strings use ==
If you want to compare numbers you the -eq (equal)
-lt (less than) -le (less than or equal to) .... etc.

so one of your conditions was something like: 

$num_tries != $max
instead it could be 

$num_tries -lt $max

hope that information helps... also I am sure there are
a ton of bash tutorials online.

best of luck.
Todd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20050626/89fd5e18/attachment.html>


More information about the ubuntu-users mailing list