BASH Scripting
Matthew S-H
mathbymath at aol.com
Sun Jun 26 19:52:34 UTC 2005
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20050626/0740b798/attachment.html>
More information about the ubuntu-users
mailing list