Help with parsing parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with parsing parameters
# 1  
Old 06-06-2006
Help with parsing parameters

Hi:- I need to parse a script 3 parameters (file, subject and email address). This is what I currently have:

allargs=$*
argcount=`echo $allargs | awk -F: '{ print NF }' ` # Total Number of arguments
pdffile=`echo $allargs | awk -F: '{ print $1 }' ` # PDF/binary file to be encoded
subject=`echo $allargs | awk -F: '{ print $2 }' ` # Parsed Subject field
reciplist=`echo $allargs | awk -F: '{ print $3}' ` # Parsed multiple/single smtp recipientlist
#outfile="`${basenam} $pdffile|sed s/out/pdf/`" # $pdffile without the path

It then does a check to make sure there are 3 argument.

This is how I am running the script:
./email_report.sh pdf_file subject email_address

but for some reason it thinks that there is only 1 argument and then it dies. But it does pickup the pdffile, subject and reciplist (as it echo's these as part of the check)

Anyone able to assist as to why it picks up the 3 arguments as 1?

Thanks
# 2  
Old 06-06-2006
couldnt get

Actually, you can refer to the parameters passed to script as $1,$2,$3 and so on.... I am not sure why you are using awk inside the script.
For a script executed as
Code:
myscript.sh first second third

the arguments are referred to inside the script as
Code:
arg1=$1  #arg1=first
arg2=$2  #arg2=second
arg3=$3  #arg3=third

Your script may be considering the 3 arguments as one because of the delimiter given to awk.
# 3  
Old 06-06-2006
Why do you do that?

if [ $# = 3 ]; then
arg1=$1
arg2=$2
arg3=$3
else
exit 1
fi
...

Perhaps what you want is to treat the last "argumet" as a (possible) list, in that case:

if [ $# > 3 ]; then
arg1=$1
shift
arg2=$1
shift
arg3=$*
else
exit 1
fi

...
# 4  
Old 06-06-2006
yes

Quote:
Perhaps what you want is to treat the last "argumet" as a (possible) list, in that case:
That could be a possibility!!!
# 5  
Old 06-07-2006
Quote:
Originally Posted by janet
allargs=$*
[snip]
Anyone able to assist as to why it picks up the 3 arguments as 1?
It could be because of the behaviour of $*. You should try using $@ instead.

Quoting from man sh

Code:
       *      Expands to the positional parameters, starting from  one.   When
              the  expansion occurs within double quotes, it expands to a sin-
              gle word with the value of each parameter separated by the first
              character of the IFS special variable.  That is, "$*" is equiva-
              lent to "$1c$2c...", where c is the first character of the value
              of  the IFS variable.  If IFS is unset, the parameters are sepa-
              rated by spaces.  If IFS is  null,  the  parameters  are  joined
              without intervening separators.
       @      Expands  to  the positional parameters, starting from one.  When
              the  expansion  occurs  within  double  quotes,  each  parameter
              expands to a separate word.  That is, "$@" is equivalent to "$1"
              "$2" ...  When there are no positional parameters, "$@"  and  $@
              expand to nothing (i.e., they are removed).

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Safely parsing parameters

I have a string like root=/dev/sda3 noacpi foo "Baz mumble" which I would like to separate into tokens like a shell does. This would be easily done with eval but that would open a security hole big enough to drop a cow through, injecting arbitrary code would be easy as pie. How can I parse this... (15 Replies)
Discussion started by: Corona688
15 Replies

2. Programming

Value changed when parsing parameters

I get a strange problem here, and ask for help. (gdb) 28 set_file_bit( file, bytePos, bitPos, argv ); (gdb) p argv $3 = 0xbfffef5c "00" (gdb) s set_file_bit (file=0x804b008, bytePos=2, bitPos=2, binary=0x80490e5 "11") at util/file.c:112 ... (2 Replies)
Discussion started by: 915086731
2 Replies

3. Shell Programming and Scripting

Help parsing job script input parameters

I have a job script that runs with input parms from the command line. job.sh -p parm1_parm2_parm3_parm4_file_1.dat The parms are separated by _ The last parm is a file name and can have an _ in the name. I currently use the following commands to extract the parms parm1=`eval echo... (3 Replies)
Discussion started by: jclanc8
3 Replies

4. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

5. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

6. Shell Programming and Scripting

More than nine parameters

Hi, please tell me the systax for passing 11 variables(including 4compulsory variables) in shell program. ORA_USERPASS=`echo $1` USERID=`echo $2` USERNAME=`echo $3` REQUESTID=`echo $4` P5=`echo $5` P6=`echo $6` P7=`echo $7` P8=`echo $8` P9=`echo $9` shift P10=`echo $9` shift... (3 Replies)
Discussion started by: anitha126
3 Replies

7. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

8. Shell Programming and Scripting

parameters

i'm supposed to come up with a script that -accepts a directory as an optional command line parameter -display an error message and terminates if more than one parameter is provided -use the current directory if no parameter is provided -displays an error message and terminates if the provided... (4 Replies)
Discussion started by: jaay
4 Replies

9. Shell Programming and Scripting

parameters

I have a script that needs to check if the given parameters are a combination of 0123456789 and not a word or another irelevant character.please help (6 Replies)
Discussion started by: aekaramg20
6 Replies

10. Shell Programming and Scripting

Parsing Parameters

How do you pass parameters over to another script and run the receiving script? . Here is an example of what I am talking about. for x in `cat Allx` do su myaccount -c "/temp/scripts/temp_script $x" > /dev/null 2>$1 $ done I was expecting the tem_script to be... (1 Reply)
Discussion started by: odogbolu98
1 Replies
Login or Register to Ask a Question