LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 12-18-2019, 11:19 AM   #1
dan-german
LQ Newbie
 
Registered: Dec 2019
Posts: 6

Rep: Reputation: Disabled
Unhappy 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
 
Old 12-18-2019, 12:12 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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"
Variable2="$2"
However, your function is also expecting two variables passed into it as shown:

Code:
AddVariable () {

Variable1="$1"
Variable2="$2"
When you later run AddVariable to call that function at end of script you are not passing any variables into it so $1 and $2 aren't populated. You should make your final line:
Code:
AddVariable $Variable1 $Variable2
Within the function you might consider changing Variable1="$1" to a different name (e.g. FVariable1="$1") to avoid confusing the variable name within the function with the similar one outside the function.
 
1 members found this post helpful.
Old 12-18-2019, 07:57 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script -- if then else elif problems Momof5.5 Linux - Newbie 5 12-05-2013 09:09 AM
[SOLVED] Elif Statement help UnixNewbie91 Linux - Newbie 6 04-28-2012 10:35 AM
if...elif...else BASH ladder problems bdrockwell Linux - Newbie 3 11-16-2010 11:38 PM
fork() not giving desired result anshu33331 Programming 4 07-29-2009 01:48 PM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration