Problem when passing argument to a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem when passing argument to a shell script
# 1  
Old 12-13-2006
Problem when passing argument to a shell script

Hi all,

I'm new to Shell scripting. In my shell script for Bourne shell, the script accepts a date parameter which is optional. If the value is supplied, the supplied value should be assigned to a variable. If not, the current date will be assigned to the variable. My script is like this.

Code:
#! /bin/sh

if [ ! -z $1 ]
then
dateval=`date`
elif [ $dateval -gt date ];then
echo "Supplied date is greater than current date"
else
 dateval=$1
fi
echo $dateval

When i run the script, it says

./shellpgm30.sh: test: argument expected

Any help would be appreciated.

Thanks,
Sumesh
sumesh.abraham
# 2  
Old 12-13-2006
Please use code tags when you are posting some code. I put your code within the tags.

In your script, the value of $dateval is not initialized in elif [ $dateval -gt date ];then. and hence you cant test it against date.

And how can you compare dates with the -gt construct ?

And what is date in that same line. Did you mean $date ?

Try this.
Code:
#! /bin/sh

if [ $# -eq 0 ] ; then
  dateval=`date`
else
  dateval=$1
fi
echo $dateval


Last edited by vino; 12-13-2006 at 06:01 AM..
# 3  
Old 12-13-2006
You are getting this error since $1 does not contain any value.Use double square brackets to avoid this problem.
Code:
if [[ -z $1 ]]

# 4  
Old 12-13-2006
Thanks Vino. The problem is solved.
Anbu,

Code:
if [[ -z $1 ]]

does not work.

It tells
Code:
./shellpgm30.sh: [[: not found

Is it the snippet for ksh.

Appreciate your help
sumesh.abraham
# 5  
Old 12-13-2006
[[ works in both ksh and bash
# 6  
Old 12-13-2006
Anbu,
Thanks for the reply.

I'm using sh. The sample code is given below.

Code:
#! /bin/sh
if [[ -z $1 ]]
then
echo "Does not contain any value"
else
 echo "Contains value"
fi

I executed the script as
Code:
 ./shellpgm32.sh

I get error as
Code:
./shellpgm32.sh: [[: not found

Can u please explain?

Thanks,
Sumesh
sumesh.abraham
# 7  
Old 12-13-2006
I think [[ is not available in bourne shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

C shell script passing arguments problem.

I found something insteresting when I tested passing arguments into my scripts. My scripts is as below. % cat passarg.env #!/bin/csh echo "passarg: argv = $argv argv = $argv" passarg1.env $* % cat passarg1.env #!/bin/csh echo "passarg1: argv = $argv argvp=$argv" set str = "test... (5 Replies)
Discussion started by: bestard
5 Replies

2. Shell Programming and Scripting

Passing argument from Java to Shell script

Hi All, I want to pass array of argument from Java to a shell script.I can use process builder api and its exec() method to call the script,but the question is how to receive the parameter in the script. Thanks in advance ---------- Post updated at 10:00 PM ---------- Previous update was... (1 Reply)
Discussion started by: Abhijeet_Atti
1 Replies

3. Shell Programming and Scripting

passing either of argument, not both in shell

Hi, I have a requirement to work on script, it should take either of arguments. wrote it as below. #!/bin/bash usage() { echo "$0: missing argument OR invalid option ! Usage : $0 -m|-r|-d } while getopts mrdvh opt; do case "$opt" in m) monitor_flag=monitor;;... (1 Reply)
Discussion started by: ramanaraoeee
1 Replies

4. Shell Programming and Scripting

Passing argument to a script while executing it within current shell

Hi Gurus, I have written a script set_env.ksh to which I pass an argument and set the oracle login credentials based on the argument I pass. The script has code as below. ORACLE_SID=$1 DB_SCHEMA_LOGON=$DB_SCHEMA_USER/$DB_SCHEMA_PASSWORD@$ORACLE_SID; export DB_SCHEMA_LOGON; echo... (3 Replies)
Discussion started by: Sabari Nath S
3 Replies

5. UNIX for Dummies Questions & Answers

Passing command output as an argument to a shell script

Hi, I have a very small requirement where i need to pass command output as an argument while invoking the shell script.. I need to call like this sh testscript.sh ' ls -t Appl*and*abc* | head -n 1' This will list one file name as ana argument.. I will be using "$1" in the shell... (2 Replies)
Discussion started by: pssandeep
2 Replies

6. Shell Programming and Scripting

Problem passing directory as argument with awk

I'm trying to figure out what's getting passed as the argument when I try to pass a directory as an argument, and I'm getting incredibly strange behavior. For example, from the command line I'm typing: nawk -f ./test.awk ~ test.awk contains the following: { directory = $NF print... (13 Replies)
Discussion started by: mrplainswalker
13 Replies

7. UNIX for Dummies Questions & Answers

Passing command line argument between shell's

Hi, I am facing a problem to pass command line arguments that looks like <script name> aa bb "cc" dd "ee" I want to pass all 5 elements include the " (brackets). when I print the @ARGV the " disappear. I hope I explain myself Regards, Ziv (4 Replies)
Discussion started by: zivsegal
4 Replies

8. Shell Programming and Scripting

passing argument to shell script that reads user inputs

Hi, Lets say I have a script "ss" which does this read abc echo $abc read pqr echo $pqr Now if I want to pass and argument to only "abc" how do I do it. If I do echo "somevalue" | ss, it does not prompt for pqr and its value comes out as blank. Any help is appreciated Thanks P (6 Replies)
Discussion started by: patjones
6 Replies

9. Shell Programming and Scripting

Problem in argument passing

Hell all, i have a problem in argument passing. print() { a=$1 b=$2 c=$3 echo $a echo $b echo $c } x="1 2 3" y="4 5 6" z="7 8 9" print $x $y $z. (4 Replies)
Discussion started by: tsaravanan
4 Replies

10. Shell Programming and Scripting

Problem with Argument Passing

Greetings, I am wrapping the monitoring commands like vmstat, sar, iostat and call via arguments I want ./unix_stats.sh -v vmstat -p <SEC> -d <Duration> to give vmstat values, and similarly iostat etc.,. Also if I give ./unix_stats.sh -v vmstat -i iostat -p <SEC> -d <Duration> should give... (4 Replies)
Discussion started by: A_Rod
4 Replies
Login or Register to Ask a Question