LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2012, 01:33 PM   #1
skydiverscott
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Rep: Reputation: 0
BASH Scripting Help


I am trying to create a script that checks for command line parameters passed to the script.

If there are no parameters passed then the script should exit with the error code 1 and print the message.

If the script was passed with only one command line parameter then I need to check that the parameter was to a valid php script. If the parameter passed was to any one of the three valid scripts then the php script is executed with any and all command line parameters also passed to the bash script.

Given the following bash script command:
Code:
myscript.sh /home/scripts/email.php --IgnoreOptin="True" --AccountNumber="987456321"
The script should evaluate and then output this:
Code:
php /home/scripts/email.php --IgnoreOptin="True" --AccountNumber="987456321"
I would need to be able to handle any quoted command line input to the shell script in the execution of the php command.

Here is my code I am starting with:
Code:
[#!/bin/bash
if [ $# -eq 0 ];
    then
		echo "No script name entered"
		echo "Enter full path to script and any command line parameters"
		exit 1
	else
		if ["$1" -eq "/home/scripts/compute.php" -o
			"$1" -eq "/home/scripts/deactivate.php" -o
			"$1" -eq "/home/scripts/email.php"];
		then
			php $@
			exit 0
		else
			echo "No valid script name entered"
			exit 2
		fi
fi
I cannot get past the second if statement's check for whether the command line input entered was equal to one of the enumerated php scripts in the bash script.
Here is my script output error:
Code:
./myscript.sh: line 8: [/home/scripts/compute.php: No such file or directory
./myscript.sh: line 9: /home/scripts/deactivate.php: Permission denied
./myscript.sh: line 10:/home/scripts/email.php: Permission denied
Not a valid script
Any help with this would be greatly appreciated.
 
Old 02-13-2012, 01:37 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Try putting a space between the [ and "$1" on line 8 and between.php" and the ] on line 10.
 
Old 02-13-2012, 01:59 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Safer to put the $@ in double quotes.
 
Old 02-13-2012, 02:54 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Code:
		if ["$1" -eq "/home/scripts/compute.php" -o
			"$1" -eq "/home/scripts/deactivate.php" -o
			"$1" -eq "/home/scripts/email.php"];
As weibullguy said (regarding the spaces), but additionally, if memory serves, the '-eq' test is for numeric comparison. You are most definitely not doing numeric comparison in these checks. You want to use:
Code:
		if [ "$1" = "/home/scripts/compute.php" -o \
			"$1" = "/home/scripts/deactivate.php" -o \
			"$1" = "/home/scripts/email.php" ];
EDIT:
The backslashes (which must not have anything--not even a space--after them) were added after your subsequent reply.

Last edited by Dark_Helmet; 02-13-2012 at 02:57 PM.
 
Old 02-13-2012, 02:55 PM   #5
skydiverscott
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Here is my updated script
(Please note that i have changed the php script location since it looks like this may be a combination of permission errors and script syntax errors and a more accurate representation of my environment may be required)

Code:
[#!/bin/bash
echo $#
if [ $# -eq 0 ];
    then
		echo "No script name entered"
		echo "Enter full path to script and any command line parameters"
		exit 1
	else
		if [ "$1" -eq "/var/www/htdocs/apps/compute.php" -o
		     "$1" -eq "/var/www/htdocs/apps/deactivate.php" -o
		     "$1" -eq "/var/www/htdocs/apps/email.php" ];
		then
			echo "$@"
			exit 0
		else
			echo "No valid script name entered"
			exit 2
		fi
fi
The script is actually located here:
/var/www/myscript.sh

The php files have mode/ownership of:
0664/user1:www-data

The script has mode/ownership of:
0770/usera:cronscript

I am logged in as user1
user one is in both www-data and cronscript groups

Here is my output now with this code:

Code:
user1@127.0.0.1:/var/www$ ./myscript.sh /var/www/htdocs/apps/email.php --IgnoreOptin="True" --AccountNumber="987456321"
3
./myscript.sh: line 9: [: missing `]'
./myscript.sh: line 10: /var/www/htdocs/apps/email.php: Permission denied
./myscript.sh: line 11: /var/www/htdocs/apps/email.php: Permission denied
Not a valid script
 
Old 02-13-2012, 03:04 PM   #6
skydiverscott
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
I think it may have to do with the mullti evaluation of the IF statement. Did the following:
Code:
[#!/bin/bash
echo $#
if [ $# -eq 0 ];
    then
		echo "No script name entered"
		echo "Enter full path to script and any command line parameters"
		exit 1
	else
		if [ "$1" -eq "/var/www/htdocs/apps/compute.php" -o "$1" -eq "/var/www/htdocs/apps/deactivate.php" -o "$1" -eq "/var/www/htdocs/apps/email.php" ];
		then
			echo "$@"
			exit 0
		else
			echo "No valid script name entered"
			exit 2
		fi
fi
And now I get this error:
Code:
user1@127.0.0.1:/var/www$ ./myscript.sh /var/www/htdocs/apps/email.php --IgnoreOptin="True" --AccountNumber="987456321"
3
./myscript.sh: line 9: /var/www/htdocs/apps/email.php: integer expression expected
Not a valid script

Last edited by skydiverscott; 02-13-2012 at 03:05 PM. Reason: Missing code tag
 
Old 02-13-2012, 03:06 PM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
./myscript.sh: line 9: /var/www/htdocs/apps/email.php: integer expression expected
Please see my earlier reply
 
Old 02-13-2012, 03:13 PM   #8
skydiverscott
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Looked that worked. Thanks!
 
Old 02-13-2012, 03:14 PM   #9
skydiverscott
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Now what about parsing the double quotes as entered on the command line? It is dropping them on output.
 
Old 02-13-2012, 03:30 PM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Bash removes them believing that the quotes were meant for it--bash.

You have two basic options.

1. Adjust your command line to protect the double quotes from bash:
Code:
./myscript.sh /var/www/htdocs/apps/email.php '--IgnoreOptin="True"' '--AccountNumber="987456321"'
or
Code:
./myscript.sh /var/www/htdocs/apps/email.php --IgnoreOptin="\"True\"" --AccountNumber="\"987456321\""
2. Adjust your bash script to examine each argument, find the '=', and insert double quotes. Perhaps something like this:
Code:
quotedOption=$( echo ${1} | sed 's@\([^=]\+\)=\(.\+\)@\1="\2"@'
Note, the above command works on the options one at a time. You would need to write a loop to go through each argument, convert it with the command above, store the result in a separate variable or "construct" a command line by appending the result to the end of the working command. Hopefully that makes sense...
 
Old 02-13-2012, 03:31 PM   #11
skydiverscott
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks.. Expedience dictates option 1A
 
  


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 Scripting Help? jhyland87 Programming 7 12-14-2008 12:58 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Help with bash scripting amishtechie Programming 5 09-28-2008 05:59 AM
Need help with bash scripting whited Programming 6 10-15-2007 09:54 PM
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 01:26 PM


All times are GMT -5. The time now is 10:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration