This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 4 years ago.
I have the following script
#!/bin/bash
if [ $1=="1" ]
then
echo $1
fi
Whenever I run ./myscript.sh 0 it still prints "0". I am not sure why? It prints whatever I type in because the if executes. What would I need to change?
Add proper spaces, i.e. before and after == inside if condition
#!/bin/bash
if [ $1 == "1" ]
then
echo $1
fi
Related
This question already has answers here:
What do $? $0 $1 $2 mean in shell script? [duplicate]
(2 answers)
What does it mean when shell command starts with a dot?
(1 answer)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Meaning of $? (dollar question mark) in shell scripts
(8 answers)
Closed 2 months ago.
In shell script in the middle there is one function as
#!/bin/ksh
var=$?
.......
FILE_NAME="/dir/user/work/file/abc.txt"
. ${FILE_NAME}
. ${HOME_PATH}/script/abc.sh
.....
func_start() {
JOB_ID=$1
FLAG=$2
NODES=$3
while [$i -le $NODES]
........ }
exit 0
What does it mean here in this script as var=$?, . ${file name} & function func_start() job_id=$1 flag=$2 nodes=$3? I am not much clear about this.
$? returns the exit value of the last executed command. echo $? prints that value on console. zero implies a successful execution while non-zero values are mapped to various reason for failure.
$1 $2 $3 : according to GNU
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using the set builtin command.
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 months ago.
I have the simplest issue, but I can't get this to work, and it's driving me crazy. So I am running a script called execute.sh that has a help function. I swear my syntax is correct below; I have tried just one equal sign and now trying two. I have tried the double brackets and continue to get the error "[-h: command not found" . To execute the script I run the following the command.
sh execute.sh -h
Where -h is the argument I am passing int that forces the bash script to call the help function and then exist. But it keeps erroring on the IF Statement. Not sure what else to do, any suggestions?
arg=$1
if ["$arg" == "-h"]; then
helpFunction
exit1;
fi
In
["$arg" == "-h"]
line, you should separate the [ from " also " from the ] with a space. Because [ is actually a command, so it should have a space after it, and the syntax of [ command arguments require a space before ].
arg=$1
if [ "$arg" == "-h" ]; then
helpFunction
exit 1;
fi
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 8 months ago.
I have a script.sh
#!/bin/bash
for i in {1..$1}
do
echo $i
done
someone could explain me why if I try
./script.sh 10 my output is {1..10}?
expected
1
2
...
10
You're echoing the parameter that you're passing in instead of printing out the loop iteration. Try:
echo $i
This question already has answers here:
How can I compare a string to multiple correct values in Bash?
(5 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 1 year ago.
I am very new to shell scripting, and i have had this error. I am using a while loop that goes a bit like this:
while [ "$variable" =! "hello" -o "$variable" =! "hi" ]
do
echo "variable isn't hi or hello"
done
but shell then gives the error [: too many arguments
This question already has answers here:
Check existence of input argument in a Bash shell script
(12 answers)
Closed 7 years ago.
I want to check if the user has given the script any arguments and if this is the case, the script should close.
if [ $# = "" ]; then
exit
fi
is not working.
You can try like this,
if [ "$#" -eq 0 ]; then
echo "Illegal number of parameters"
fi