Meaning of '$#' in Unix | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Meaning of '$#' in Unix

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-09-2011
Oracle_User Oracle_User is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 17 May 2013, 4:17 AM EDT
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
Meaning of '$#' in Unix

All,

In the below mentioned piece of code :


Code:
if test $# -eq 1
        then
            echo "Input parameter passed into DMI_weekly.ksh..." | tee -a $RUNLOG
            typeset -u ORACLE_SID
            export ORACLE_SID="$1"
        else
            echo "ERROR 060: Arguments passed to DMI_weekly.ksh do not equal 1..." | tee -a $RUNLOG
            echo "***Verify that a valid ORACLE_SID is being passed when calling script..." >> $RUNLOG
            echo "See $RUNLOG for more details..."
            echo "\nExiting DMI_weekly.ksh with exit status=1...\n"| tee -a $RUNLOG
            echo "Preload Error:  Arguments passed to DMI_weekly.ksh do not equal 1" |mailx -s "DMI_weekly.ksh (ERROR 070)" "
$MAILID"
        exit 1
    fi

what '$#' tells to the unix ...?


Regards
Oracle user

Last edited by DukeNuke2; 03-09-2011 at 02:13 AM..
Sponsored Links
    #2  
Old 03-09-2011
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 17 October 2014, 9:03 AM EDT
Location: Mumbai, India
Posts: 1,614
Thanks: 125
Thanked 184 Times in 178 Posts
It is the special built-in variable which contains the total number of parameters passed to any shell script.

if you invoke script test.ksh in this way


Code:
./test.ksh arg1 arg2 arg3

then the value of $# inside the script would be 3.
Sponsored Links
    #3  
Old 03-09-2011
fhernu fhernu is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 9 March 2011, 5:14 PM EST
Posts: 10
Thanks: 1
Thanked 1 Time in 1 Post
This means the number of parameters (values) given to the script.
This small example will help you to understand :

Create the scritpt test.sh containing the following lines
echo "1st parameter : $1"
echo "2nd parameter : $2"
echo "3rd parameter : $3"
echo "Number of parameters: $#"
If you execute ./test.sh A B C

The output will be :
1st parameter : A
2nd parameter : B
3rd parameter : C
Number of parameters: 3
If now you execute ./test.sh A B

The output will be :
1st parameter : A
2nd parameter : B
3rd parameter :
Number of parameters: 2
Coming back to your script :

if test $# -eq 1 checks that one parameter is given to the script during its execution. Please notice this doesn't check the content of this parameter.
The Following User Says Thank You to fhernu For This Useful Post:
Oracle_User (03-09-2011)
    #4  
Old 03-09-2011
Oracle_User Oracle_User is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 17 May 2013, 4:17 AM EDT
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
Thank-you anchal and fhernu.
Even i was considering it in the same way, putted this in the forum, just to confirm it.

Regards
Oracle User
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
^$$ meaning Pratik4891 Shell Programming and Scripting 8 12-21-2010 04:55 PM
What is the meaning of $_ giri_luck Shell Programming and Scripting 4 01-05-2010 02:11 AM
Oot: Level 2 Unix Support? meaning blesets UNIX for Dummies Questions & Answers 0 05-08-2007 06:28 AM
Meaning of ; in UNIX file? willdaw3 UNIX for Dummies Questions & Answers 1 01-11-2005 01:46 PM
Meaning of unix-rt ldp file type jfmrts UNIX for Dummies Questions & Answers 1 10-14-2004 04:40 PM



All times are GMT -4. The time now is 03:05 AM.