arguments expected error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arguments expected error
# 1  
Old 05-18-2005
arguments expected error

ive implemented getopt for the command line but have this problem,
Code:
#!/bin/sh
text=""
set -- getopt "t" etc ....    #sets arguments
while :
do
   case "$1" in                  #gets arguments
   -t: shift; text="$1" ;;
   shift
done
shift

if [ $text = "" ]
then
   echo "no text"
else
   echo "text"
fi

when i run the program ./script -t <text>
it prints "text"
but when i just run ./script
it returns a "arguments expected" message, (shouldnt it return "no text" ?)
is there a way to fix this, i want to be able to run ./script without it complaining as well as ./script -t <text>
# 2  
Old 05-18-2005
Two things that came to my notice.

Since you are using case statements, you should close the case statements with esac

Your script would now look like this:
Code:
do
   case "$1" in                  #gets arguments
   -t: shift; text="$1" ;;
   esac
done

Regarding your use of shift indicates that the script requires you to input arguments. From the man pages we have the following:

Code:
 shift [n]
              The  positional  parameters  from n+1 ... are renamed to $1 ....
              Parameters represented by the numbers  $#  down  to  $#-n+1  are
              unset.   n  must  be a non-negative number less than or equal to
              $#.  If n is 0, no parameters are changed.  If n is  not  given,
              it  is assumed to be 1.  If n is greater than $#, the positional
              parameters are not changed.  The return status is  greater  than
              zero if n is greater than $# or less than zero; otherwise 0.

Also your sciprt has a $1 which actually in scipt-terminology refers to the first argument.

No arguments + your script = "arguments expected"

It is always a good idea to check whether the user has provided any arguments at all.

$# gives you the count of the number of arguments provided at command line.

You script should be tweaked like this:

Code:
#! /bin/sh

if [ $# -eq 0 ] ; then
   echo "No text"
else
while :
do
   case "$1" in                  #gets arguments
   -t: shift; text="$1" ;;
   shift
done
shift
   echo "Text"
fi

# 3  
Old 05-18-2005
thanks for that, that was quite informative.
just wandering how would that script be modified if say there
were multiple arguments?
e.g
arguments "t:w"
if theres no -t
do something
else
do something else
if theres no -w
do something
else
do something else
etc ...
# 4  
Old 05-18-2005
# 5  
Old 05-18-2005
thanks for that, it does help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

2. Shell Programming and Scripting

ERROR: `(' is not expected.

Hi All, I have written a shell script which works all right on bash shell, but when it comes to execute it using ksh on AIX it gives the following error::( bash$ /bin/ksh getShortInfo.sh getShortInfo.sh: syntax error at line 26 : `(' unexpected Could you please indicate what is... (4 Replies)
Discussion started by: Elvis
4 Replies

3. Programming

Please Help ! ----> error: expected ‘=’,

#include<stdio.h> int main{ char *fl; fl=(char*)malloc(150); strcat(fl,"/tmp/OV/"); printf("\nInside fl--->%s\n",fl); return 0; } I wrote a simple program as above. I got the error error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token Please help me out ! I am... (4 Replies)
Discussion started by: gameboy87
4 Replies

4. Shell Programming and Scripting

Error: integer expression expected

root@server01 # df -h | grep /tmp | awk {'print $3}' 252M root@server01 # root@server01 # cat /usr/local/tmpchk.sh #!/bin/sh x=`df -h | grep /tmp | awk {'print $3}'` if ; then rm -fr /tmp/somefolder/ else echo "its small" (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies

5. Shell Programming and Scripting

error : test: argument expected

Hello all, I am trying to figure out why i am getting an error while executing the script...altought it seems like its work...but still get the test arguement error...any help would be appericiate...this script basically connects to any oracle db ( just have to pass db name to it)... (4 Replies)
Discussion started by: abdul.irfan2
4 Replies

6. Shell Programming and Scripting

if stmt argument expected error

CT=0 while read LINE do # Check to see if the LINE is non-empty, and has a <td> tag in it. if then # Increase the TD counter by 1 CT=`echo "$CT+1"` fi done <test.htmthrows this error: ksh: test: argument expected test.htm <tr> <td>text</td... (4 Replies)
Discussion started by: dba_frog
4 Replies

7. Shell Programming and Scripting

fi not expected error

I'm trying this script and I keep getting a 'fi' not expected error: #!/bin/sh #TD=0 CT=0 cat P7748 |while read LINE do # Check to see if the LINE is non-empty, and has a <td> tag in it. if # Increase the TD counter by 1 CT=`echo "$CT+1" |bc` ... (2 Replies)
Discussion started by: dba_frog
2 Replies

8. Shell Programming and Scripting

Receiving error: ./ang.ksh[35]: 0403-057 Syntax error at line 116 : `done' is not expected.

Hi All I am quite new to Unix. Following is a shell script that i have written and getting the subject mentioned error. #!/bin/ksh #------------------------------------------------------------------------- # File: ang_stdnld.ksh # # Desc: UNIX shell script to extract Store information.... (3 Replies)
Discussion started by: amitsinha
3 Replies

9. Shell Programming and Scripting

syntax error near un expected token =~

Hello. Please help with this code. Returns an error message "syntax error near unexpected token =~. Conditional binary operator expected. if " ]] || " ]] then echo "Enter only valid numbers" fi (2 Replies)
Discussion started by: Pauline mugisha
2 Replies

10. Shell Programming and Scripting

ERROR: ./launch_full_backup.sh[18]: Syntax error at line 28 : `else' is not expected.

Help please! :confused: I have the following error with the following file and the emails are not arriving to the email, any idea please? ERROR: ./launch_full_backup.sh: Syntax error at line 28 : `else' is not expected. FECHA=`date +%d%m%y%H%M`... (2 Replies)
Discussion started by: villenan
2 Replies
Login or Register to Ask a Question