LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with Bash script (https://www.linuxquestions.org/questions/programming-9/need-help-with-bash-script-83039/)

m4rccd 08-18-2003 03:31 AM

Need help with Bash script
 
#!/bin/bash
#####################################
#Version and Code variables (DONT CHANGE)#
VER="Itaka 1.3"
CODE="Costumbres Argentinas"
MAIN="/usr/lib/itaka"
##########START ITAKA FILE CHECKING AND PRE-RUN#########

clear
#if [ -d $MAIN ]; then
#if [ -f ~/.itaka/itaka.config ]; then
#source ~/.itaka/itaka.config
#else
#(
# mkdir ~/.itaka/
# cp $MAIN/config/itaka.config-default ~/.itaka/itaka.config
# )
#source ~/.itaka/itaka.config
#fi

# Checking for /var/www or Itakas root, check ~/.itaka/itaka.config

test -d "$directory" || mkdir "$directory"

for file in index.php help.php; do test -f "$directory/$file" || cp
"$MAIN/files/$file" "$directory/"; done



#############USAGE FUNCTION##################
func_usage () {
echo -e "\e]0;$VER ($CODE)\a"
echo "$(date +"%D %X %p" ): Itaka initiated by $(whoami)" >>
~/.itaka/itaka.log
echo
echo " $VER ($CODE) "
#echo -e "\e[1;31m $VER ($CODE) \e[0m"
echo " Running on $directory "
echo
cat <<- EOF
Usage: itaka -s (Usage of advanced settings is available)

-v|--version Show Itaka's version
-s|--start|-start Start Itaka with default options.(Recommended)
-c|--configure|-configure Show configure menu
-r|--readme|-readme Show Itaka's Readme file



Advanced options, to override defaults. (Advanced users only):



-n|--number|-number Specify how many screnshots you want to take,
default is unlimited.

-t|--time|-time Specify the time between each screenshot,
default is "8" (8 is recommended).

-x|--command|-command Specify the command you want to use to take the
screenshot, default is "scrot -q 90".
(Scrot is recommended). Dont put the image name.

-d|--dir|-dir Specify the directory you want Itaka to place the
index and image files. Default "/var/www/itaka".
Change it if you want.

-h|--sh|-sh Show options in ~/.itaka/itaka.config.

-rd|--rest|-rest Restore default options.

-sa|--save|-save Set the options you just typed as default.
options will be saved to ~/.itaka/itaka.config.

EOF

}





#############START FUNCTION##################
func_start ()
{
cat<<STARTMSG
Press Control-C to quit
Logging all activity to ~/.itaka/itaka.log
Start your webserver, and point your browser to localhost/itaka/ (if
default directory variable is set, check the ~/.itaka/itaka.config file)
STARTMSG
while true; do
# \e[1;1H
for ((i="$time"; i>0; i--)); do
echo -en "\r$i seconds left until next screenshot."
sleep 1
done
echo
$command $directory/image.jpg
echo -e -n "$(date +"%X %p" ): Screenshot N:$((number++)) taken\r"
echo
date +"%D %X: Screenshot N:$number taken" >> ~/.itaka/itaka.log
done
}


#########CASE -OPTIONS######################
case $1 in
-start|-s) func_start;;
-readme|-r) more $MAIN/docs/README;;
-configure|-c) func_configure;;
-number|-n) VALUE=$2; func_number;;


-time|-t) time="$2"; shift 2;;
-command|-x) command="$2"; shift 2;;
-dir|-d) directory="$2"; shift 2;


--version|-version|-v) echo "$VER ($CODE)";;
#*) error_code=$1; if [ -z $error_code ] ; then func_usage; else echo "Sorry
option "$error_code" doesnt exist."; func_usage; fi;;
*) [ -z "$1" ] || echo "sorry option "$1\" does not exist.";
func_usage;;
esac

shift


else
echo
echo "Cant find $MAIN, Install Itaka please!, or check the script to see if the
MAIN variable is set correctly!"
echo
fi


===========================


My question is,
func_start needs 3 variables to work, $command $directory and $time.. Those variables are suppose to be put in ./script -start -t 8 -dir /var/ww/itaka -x scrot -q 90

But somehow the case thing doesnt parse them ??? Can anyone help im totally lost....

./script -order of options doesnt matter

Can anyone help?
Using export to set them global didnt work either, any help is appreciated

crabboy 08-18-2003 12:37 PM

The problem is according to your script the order of the options _does_ matter. When the script begins to process the options, it talks -start as the first arg and calls start. What you should do is only set variables in the command option case statement and act on the variables after you are done with all of them.

For example:

currently you have

-start|-s) func_start;;

You should have

START_OPT=0


-start | -s ) START_OPT=1;;



and when done processing args


if [ $START_OPT -eq 1 ]; then
func_start
fi

m4rccd 08-18-2003 04:00 PM

while [[ $# != 0 ]]; do

START_OPT=0
case $1 in
-start | -s ) START_OPT=1;;
-readme|-r) more $MAIN/docs/README;;
-configure|-c) func_configure;;
-number|-n) VALUE=$2; func_number;;
-time|-t)time=$2; shift 2;;
-command|-x) command=$2; shift 2;;
-dir|-d) directory=$2; shift 2;;
--version|-version|-v) echo "$VER ($CODE)";;
*) [ -z "$1" ] || echo "sorry option \"$1\" does not exist."; func_usage;;
esac
#*) error_code=$1; if [ -z $error_code ] ; then func_usage; else echo "Sorry opt#ion "$error_code" doesnt exist."; func_usage; fi;;
shift
done


if [ $START_OPT -eq 1 ]; then
func_start
fi





Is that allright? you didnt specify where to put them :scratch:

crabboy 08-18-2003 09:51 PM

Do it for all the flags.
Code:

START_OPT=0
README_OPT=0
... rest of varables used below

case $1 in
-start | -s ) START_OPT=1;;
-readme|-r) README_OPT=1;;
-configure|-c) CONFIGURE_OPT=1;;
-number|-n) NUMBER_OPT=1; NUMBER_VALUE=$2; shift 2;;
-time|-t) TIME_OPT=1; TIME_VALUE=$2 ; shift 2;;
-command|-x) COMMAND_OPT=1; COMMAND_VALUE=$2; shift 2;;
-dir|-d) DIRECTORY_OPT=1; DIRECTORY_VALUE=$2; shift 2;;
--version|-version|-v) VERSION_OPT=1;;
*) [ -z "$1" ] || echo "sorry option \"$1\" does not exist."; func_usage;;
esac
#*) error_code=$1; if [ -z $error_code ] ; then func_usage; else echo "Sorry opt#ion "$error_code" doesnt exist."; func_usage; fi;;
shift
done

down below process the args in the order you want with if conditions.

m4rccd 08-19-2003 06:00 PM

Projects/itaka/1.3/files/Itaka.sh: line 226: [: =: unary operator expected
Projects/itaka/1.3/files/Itaka.sh: line 230: [: =: unary operator expected
Projects/itaka/1.3/files/Itaka.sh: line 234: [: =: unary operator expected
Projects/itaka/1.3/files/Itaka.sh: line 238: [: =: unary operator expected
Projects/itaka/1.3/files/Itaka.sh: line 242: [: =: unary operator expected
Projects/itaka/1.3/files/Itaka.sh: line 246: [: =: unary operator expected


I tried changing -eq to =
I tried setting the 1 to "1"
Any ideas?

crabboy 08-20-2003 09:49 AM

You will need to either initialize all the variables used as option flags statement or test each with a -z before comparing the values.

Code:

START_OPT=0
README_OPT=0
CONFIGURE_OPT=0
NUMBER_OPT=0
TIME_OPT=0
COMMAND_OPT=0
DIRECTORY_OPT=0

Also, not sure why you are using shift 2 if the option has a parameter. I would think that one shift will do and the second will be taken care of before the end of the while loop.


All times are GMT -5. The time now is 03:17 PM.