LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2003, 03:31 AM   #1
m4rccd
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Rep: Reputation: 0
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
 
Old 08-18-2003, 12:37 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 08-18-2003, 04:00 PM   #3
m4rccd
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 08-18-2003, 09:51 PM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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.
 
Old 08-19-2003, 06:00 PM   #5
m4rccd
LQ Newbie
 
Registered: Aug 2003
Posts: 10

Original Poster
Rep: Reputation: 0
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?
 
Old 08-20-2003, 09:49 AM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script Linh Programming 4 04-21-2004 05:19 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script brian0918 Programming 7 06-12-2003 06:06 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration