Bash Linux - call function in a if statement [duplicate] - linux

This question already has answers here:
if, elif, else statement issues in Bash
(5 answers)
Closed 3 years ago.
i have probably some nooby question
how can I call a defined function in a if statement in bash?
Have a look on my example:
#!/bin/bash -x
Variable_A=xyz
function_x() {
echo "hello everyone"
}
#here I want to define a if statement after the function
if Variable_A == working;then
function_x()
#here I have a while for the parameters in bash example:
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--status)
Variable_A="$2"
shift
shift
;;
esac
done
so the idea is when I run the script let's call it test.sh it runs the script exp in bash:
./test.sh working
I am not sure how to create something like this, maybe some of you can help me out on this.
Thank you in advance

Your if should look like:
#here I want to define a if statement after the function
if [ "$Variable_A" = "working" ]
then
function_x
fi
You need to end every if statement with fi and functions are called without ().
https://www.thegeekdiary.com/bash-if-loop-examples-if-then-fi-if-then-elif-fi-if-then-else-fi/
Another important note on if statements. You need a space before and after [ or ].
Your if logic should also be after the script parms are read and Variable_A is set.

Related

How to get variables config from shell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
cat a.sh
#!/bin/bash
t=1232
echo $1
#echo $t
when i run the script "./a.sh $t" I can't get value 1232
when the script replacement with echo $t ,run the script "./a.sh" can get vaule 1232
can anybody else tell me ,if i use "./a.sh $t" this form ,how can get the vaule,thanks alot
have no ideas to get the variables throug the termi
When you run "./a.sh $t your current shell evaluates $t to '' so $1 is unset in your script and it will just execute echo.
If you quote the the variable either ./a.sh \$t or ./a.sh '$t' your script will do echo '$t'. You can then use either eval to get it to evaluate the expression:
eval echo "$1"
or preferable strip off the leading '$' and use indirect variable:
var=${1:1}
echo "${!var}"
If you just need to store data use json or sqlite instead of a script.
If you have logic in your script consider just passing in the variable names and if not set dump all variables (using the name convention that your variables are lower case):
#!/bin/bash
if [ -z "$1" ]
then
set | grep "^[a-z]"
exit 0
fi
for v in "$#"
do
set | grep "^$v="
done
and you then do:
$ ./a t
t=1232
$ ./a
t=1232
I think your variable t are out of scope. Therefore, what you want to do is assign the variable t=1232, beforehand, and use it as an argument. So the script would be
#!/bin/bash
echo $1
Then call the script as you wanted to with variable t already assigned to the value, so it would print the desired output
t=1232
./script.sh $t
I think that's that. would love to hear your thoughts tho

How would I write an 'if' command that checks to see if there are multiple arguments?

I need to create a script that uses an 'if' command that checks whether there is exactly one argument. If there is more than one argument, I need it to echo “Usage: give exactly 1 argument, the string to be looked for” and then exit immediately.
Welcome to Stackoverflow.
This should do the trick:
if [ "$#" -gt 1 ]; then
echo "Usage: give exactly 1 argument, the string to be looked for"
exit 0
else
echo the expected processing happens in this section of this code
fi

How to validate user input on a bash script? [duplicate]

This question already has answers here:
Validate the number of arguments passed in bash from read
(2 answers)
Closed 2 years ago.
I'm creating a bash script. However, I need the script to validate that the user has to enter two arguments. Not 1 and nothing more than 3.
echo -n "Enter two values"
read val1 val2
try this:
#!/bin/bash
if [ $# -eq 2 ]; then
echo "var1: $1 var2: $2"
else
echo "error: need to set two argument"
fi
run:
./scriptName.sh first second

Arguments with space when parsing input parameters for bash script

I wrote a bash script which takes parameters by name and read its value in key value pairs. Below is an example of how I run my script.
sh test.sh param1='a' param2='b'
I then refer the input arguments like $param1 and $param2 inside my script. Below is the script I wrote to do this.
for item in $#; do
case $item in
(*=*) eval $item;;
esac
done
But when there is space in the value of the argument, it takes only the part before the space.
param3='select * from'
would only assign 'select' to param3 and not the whole string. How can I achieve this? Any help would be much appreciated.
EDIT :
After Inian's answer
for item in $#; do
case "$item" in
(*=*) eval "$item";;
esac
done
You can do this by quoting "$#" and using declare instead of eval:
for item in "$#"; do
case $item in
(*=*) declare "$item" ;;
esac
done
However, this is now a bash script, so you have to run it with bash test.sh and not sh test.sh

utterly confused regarding bash script command line arguments

I have the following bash script file callee.sh which is being called from another script file caller.sh.
The callee.sh is as follows:
if [ $1 -eq 1 ];
then
echo inside $1
source ~/MYPROGRAMSRC/standAloneWordCount.sh $2
#echo "inside standalone branch"
#echo $1
elif [ $1 -eq 2 ];
then
#echo "inside distributed branch"
#echo $1
else
echo invalid option for first argument-\n Options:\n "distributed"\n or\n "standalone"\n
fi
As most people might be able to tell, this is a script I use to decide whether to run hadoop in distributed or standAlone mode depending on the arguments.
This script is called from caller.sh as follows
source callee.sh $2 $counterGlobal
where $2 is a number either 1 or 2 and $counterGlobal is some integer.
My problem is that the if condition in callee.sh never evaluates to True and hence my script standAloneWordCount.sh which I call from within callee.sh is never called. I am running with bash shell and have tried many variants of the if statement like:
if [ $(($1 == 1 )) ] -- (1)
In an echo statement just above the line -- (1) , the expression $(($1 == 1)) evaluates to 1 so I am baffled as to why I am unable to satisfy the if condition.
Also I keep getting the error where it says:
syntax error near unexpected token `else'
if anyone could help me out with these two errors, it would be much appreciated. As I've run out of ideas.
Thanks in advance!
have tried many variants of the if statement like:
if [ $(($1 == 1 )) ]
You should instead be saying:
if (($1 == 1)); then
...
fi
Regarding the Syntax error near unexpected tokenelse'`, it's not because of any code that you've shown above. It seems to originate from some other portion of your script.
If you're using bash, try using double square brackets:
if [[ $1 -eq 1 ]]; then
echo "inside 1"
fi
As for the syntax error, you need quotes around your text (which also means escaping the existing quotes or use single quotes):
echo -e "invalid option for first argument-\n Options:\n \"distributed\"\n or\n \"standalone\"\n"
The -e flag is there to let bash know you want the \n to evaluate to a newline.

Resources