Special symbols in shell scripting


 
Thread Tools Search this Thread
Operating Systems Solaris Special symbols in shell scripting
# 1  
Old 01-18-2009
Special symbols in shell scripting

I am new to unix shell scripting, i was going through the existing shell scripts but couldn't able to get enough information on below syntaxes,i mean the symbols $# & $? used in the if loop. what exactly are they? what is the inline meaning, Could you please throw some light.

Examples:

1)
if [ $# -eq 3 ]
then
numFiles=`cat $3 | wc -l | sed 's/ //g'`
#echo $numFiles
count=0

2) if [ $? -eq 0 ]
then
echo "File $line moved to archive on `date +%D`">>$logFile
#echo $'\n' >>$logFile
else
echo "File $line can not be deleted">>$logFile
fi
# 2  
Old 01-18-2009
Extracted from Solaris Bourne shell man page:

Quote:
# The number of positional parameters in
decimal.

- Flags supplied to the shell on invocation or
by the set command.

? The decimal value returned by the last syn-
chronously executed command.
In the examples:

1. If the number of parameter is equal to 3, then...

2. If the error code of previous application is 0, then...
# 3  
Old 01-19-2009
Quote:
Originally Posted by Ariean
I am new to unix shell scripting, i was going through the existing shell scripts but couldn't able to get enough information on below syntaxes,i mean the symbols $# & $? used in the if loop. what exactly are they? what is the inline meaning, Could you please throw some light.

Examples:

1)
if [ $# -eq 3 ]
then
numFiles=`cat $3 | wc -l | sed 's/ //g'`
#echo $numFiles
count=0
$# stores the count of command file arguments. This 'IF' check ensures that there should be exactly 3 command line arguments including the script name itself

Quote:
2) if [ $? -eq 0 ]
then
echo "File $line moved to archive on `date +%D`">>$logFile
#echo $'\n' >>$logFile
else
echo "File $line can not be deleted">>$logFile
fi
$? stores the status of last system call. $? -eq 0 means that the last command was successfully executed
# 4  
Old 01-19-2009
I created the following script file and was getting an error could you please let me know where i am wrong. My idea is to check whether the for loop succeeded in creating or appending data into the file if so display a success message. Though the file has been created successfully my IF loop is not working properly it seems.

######
for i in `ls -rt *_FI.csv`
do
echo $i >> $currdir/finance.list
done

if [$? -eq 0]
then
echo "Finance list file has been generated"
else
echo "Error in preparing the Finance list file"
fi
##############

Error Message:
./gpm.ksh[17]: [0: not found
Error in preparing the Finance list file
# 5  
Old 01-19-2009
I dont know about ksh, but in Bourne or Bourne-alike shells, the correct syntax for if condition is:

Code:
if [ $? -eq 0 ]

You need whitespace after and before square bracket.

Btw, you should do all your shell scripting jobs in Bourne (sh) shell Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell scripting to determine special chars in file

Hi, I need all your help to achieve the below functionality. I have a big 2 GB file and inside the file we need to identify, whether having a comma(,) or pipe(|) or tab or fixed position or semicolon(;) delimiter. If any of those delimiter found need to replace the file with pipe(|)... (1 Reply)
Discussion started by: lkeswar
1 Replies

2. UNIX for Dummies Questions & Answers

Count special symbols between each unit

Can anybody help me figure this out? Thank you in advance. I have a input file. It shows like this: Query= random content random content > random content random content > random content > random content Query= random content random content random content > random content > random... (1 Reply)
Discussion started by: yuejian
1 Replies

3. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

4. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

5. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

Run a command in a special shell

Hi there, Imagine we have to run a command in python shell from a perl script. 1 #!/usr/bin/perl 2 use strict; 3 my @con; 4 @con = `python`; 5 #?????print `print 'salaam'`;???? What's suitable situation for fifth line? Thanks in advance. (4 Replies)
Discussion started by: Zaxon
4 Replies

8. Shell Programming and Scripting

Urgent: How to eliminate special symbols in a file and how to read it

Hi, I have file called suppliersList.txt --------------------------------- 112|MIMUS|krish@google.com 113|MIMIRE|krish@google.com 114|MIMCHN|krish@google.com 115|CEL|krish@google.com 108|UGEN|krishn@google.com 109|SLAND|krish@google.com i have 3 varibale ------------- no Name... (4 Replies)
Discussion started by: kittusri9
4 Replies

9. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

10. Shell Programming and Scripting

Special Character Check in Shell script

Hi, I'm currently working on a project that requires parsing xml file. One of the field in the xml is shown below (don't remember exactly): <variable="ITEM">12345678</variable> I coded my script keeping in mind that the value denoted in bold will always be a number. After getting just the... (1 Reply)
Discussion started by: mradul_kaushik
1 Replies
Login or Register to Ask a Question