The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script $0 argument painulyarun UNIX for Dummies Questions & Answers 2 03-25-2008 07:32 PM
Problem with Argument Passing A_Rod Shell Programming and Scripting 4 09-13-2006 11:47 AM
Passing argument from one script to other lloydnwo Shell Programming and Scripting 3 10-27-2005 02:06 PM
shell script argument parsing rmjoe Shell Programming and Scripting 1 07-28-2005 03:37 PM
Passing argument to awk script AkumaTay UNIX for Dummies Questions & Answers 1 08-03-2001 11:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-13-2006
sumesh.abraham
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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
  #2 (permalink)  
Old 12-13-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 (permalink)  
Old 12-13-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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 (permalink)  
Old 12-13-2006
sumesh.abraham
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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
  #5 (permalink)  
Old 12-13-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
[[ works in both ksh and bash
  #6 (permalink)  
Old 12-13-2006
sumesh.abraham
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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
  #7 (permalink)  
Old 12-13-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
I think [[ is not available in bourne shell.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0