my if elif else statement in bash is not giving me the desired result
my question is:
1) i wrote an if,elif,else statement which ran successfully and gave me the desired output,but when i placed that same statement in a bash function,it did not run successfully. i can not figure out where am getting wrong.please can someone help me go through my bash script,thank you. 1) This if,elif,else statement ran succesfully #!/bin/bash Variable1="$1" Variable2="$2" if [ "$#" -eq 0 ] then echo "provide an argument" elif [ "$#" -ne 1 ] && [ "$#" -ne 2 ] then echo "Surplus argument provided" else echo "have got your back" fi 2) When i put the above statement in a bash function,it did not give me the desired results. #!/bin/bash Variable1="$1" Variable2="$2" AddVariable () { Variable1="$1" Variable2="$2" if [ "$#" -eq 0 ] then echo "provide an argument" elif [ "$#" -ne 1 ] && [ "$#" -ne 2 ] then echo "Surplus argument provided" else printname "$Variable1" deletename "$Variable2" fi } deletename(){ for file in $Variable2/*;do echo "$(rm -i "$file")" echo "deleting $Variable" done } printname(){ Variable=$1 for file in $Variable1/*;do echo "$(basename "$file")" done } #printname "$Variable1" #deletename "$Variable2" AddVariable please i am new to bash scripting thank you |
Positional parameters ($1, $2, $3, etcc...) are relative to what they're being passed into.
On invocation of your script you're using $1 and $2 as passed into the script at invocation which is correct. Code:
Variable1="$1" Code:
AddVariable () { Code:
AddVariable $Variable1 $Variable2 |
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
|
All times are GMT -5. The time now is 04:27 AM. |