Getting number of argument passed to a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting number of argument passed to a shell script
# 1  
Old 09-12-2017
Getting number of argument passed to a shell script

Hi Experts,

I have been trying to work on a simple shell script that will just add the two argument passed to it. Here is what i tried :
Code:
#!/bin/bash
welcome(){
echo "Welcome to this Progg. which will accept two parameter"
}

main_logic(){
 arg=$#
 echo "Number of argument passed is $arg"
 first_arg=$1
 second_arg=$2
 echo "First argument is $first_arg"
 echo "Second argument is $second_arg"
}

welcome
main_logic

Below is the output which isn't correct :-
Code:
Welcome to this Progg. which will accept two parameter
Number of argument passed is 0
First argument is 
Second argument is

Any idea what am I doing wrong?
# 2  
Old 09-12-2017
you need to give the parameters of the script to the call of the function:

Code:
main_logic ${@}


Last edited by Scrutinizer; 09-12-2017 at 01:46 PM.. Reason: code tags
# 3  
Old 09-12-2017
And you must put double quotes around it.
Code:
main_logic "$@"

# 4  
Old 09-12-2017
Quote:
Originally Posted by mukulverma2408
Any idea what am I doing wrong?
Yes, and as you have already received the solution i wil concentrate on explaining the reason:

When you invoke a process with some arguments these arguments become part of the "process environment". Inside a script you can then use special variables ("$@", "$*", "$1", "$2", ...) to pull these arguments out of the environment or get ("$#") the number of parameters passed.

But every time you invoke a shell function inside a script this function gets its own environment of sorts and all the special variables "$1", "$2", etc. and "$#" describe what is passed as argument to the function instead of what is passed to the main script. Look at the following sample script:

Code:
#! /bin/bash

subfunc()
{
    echo "inside subfunc"
    echo "Number of arguments passed: $#"
    echo "argument 1: $1"
    echo "argument 2: $2"
    echo "argument 3: $3"
}

# main part
echo "inside main"
echo "Number of arguments passed: $#"
echo "argument 1: $1"
echo "argument 2: $2"
echo "argument 3: $3"

subfunc a b c
subfunc X Y

And its output will be:

Code:
# ./sample.sh foo bar baz blah
inside main
Number of arguments passed: 4
argument 1: foo
argument 2: bar
argument 3: baz
inside subfunc
Number of arguments passed: 3
argument 1: a
argument 2: b
argument 3: c
inside subfunc
Number of arguments passed: 2
argument 1: X
argument 2: Y
argument 3:

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 09-13-2017
Thanks @bakunin, that was useful
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. Shell Programming and Scripting

Variable passed as argument

I have a script. #!/bin/sh cur_$1_modify_time=Hello echo "cur_$1_modify_time" When I run like sh /root/script1 jj I expect value "Hello" being assigned to variable "cur_jj_modify_time" and output being "Hello" ie echoing $cur_jj_modify_time But the output comes as # sh... (3 Replies)
Discussion started by: anil510
3 Replies

3. Shell Programming and Scripting

Shell script that check the argument passed to it and prints error if test condition is not met

I want to make a script that check for the argument passed to it and generates an error in case any character/string argument passed to it. I am using below code, but its not working. can anyone help. #!/bin/bash if ]; then echo 'An integer argument is passed to the script hence... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

4. Shell Programming and Scripting

Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through... Script that i am using is below : #!/bin/bash sum=0 for i in $@ do sum=$sum+$1 echo $sum shift done I am executing the script as... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

5. Shell Programming and Scripting

how to specify number of argument in shell

Hi I am new in shell, I am trying to create a small script that can do exit if a script is executed when argument not 2 #!/bin/sh if ; then echo greater exit 1; elif ; then echo less exit 1; fiit keeps returning me whatever number of argument I... (1 Reply)
Discussion started by: peuceul
1 Replies

6. Shell Programming and Scripting

shell script for ftp files passed in command line argument

i want to write a shell script function that will ftp the files passed in the command line . i have written a shell script for ftp but how will it do for all files passed in command line argument , i am passing 4 files as argument ./ftp.sh file1 file2 file3 file4 code written by me... (5 Replies)
Discussion started by: rateeshkumar
5 Replies

7. Shell Programming and Scripting

File not recognized when passed as argument

I have the below script in file read_file.ksh if ] || ] then echo "Required one input file" echo "Enter a file to get char count:" read $FILE_NAME if ] then echo "valid file" else echo "Not a valid file." fi When run as read_file.ksh detail.csv or... (9 Replies)
Discussion started by: michaelrozar17
9 Replies

8. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

9. Shell Programming and Scripting

how to print all argument passed

like i have script with which i have passed arg list :eg: i/p: scriopt1 arg1 arg2 arg3 .... argn o/p: arg1 arg2 arg3 .... argn (2 Replies)
Discussion started by: RahulJoshi
2 Replies

10. Shell Programming and Scripting

Check if argument passed is an integers

How do I check if the argument passed to a script is an integer? I am writting a script that will take to integers and want to be able to check before I go on. I am using bourne shell. Thanks in advance (13 Replies)
Discussion started by: elchalateco
13 Replies
Login or Register to Ask a Question