if arguments is equal to this string, define a variable like this string
Solution 1
Don't forget about spaces:
source=""
samples=("")
if [ $1 = "country" ]; then
source="country"
samples="US Canada Mexico..."
else
echo "try again"
fi
Solution 2
You can use either "=" or "==" operators for string comparison in bash. The important factor is the spacing within the brackets. The proper method is for brackets to contain spacing within, and operators to contain spacing around. In some instances different combinations work; however, the following is intended to be a universal example.
if [ "$1" == "something" ]; then ## GOOD
if [ "$1" = "something" ]; then ## GOOD
if [ "$1"="something" ]; then ## BAD (operator spacing)
if ["$1" == "something"]; then ## BAD (bracket spacing)
Also, note double brackets are handled slightly differently compared to single brackets ...
if [[ $a == z* ]]; then # True if $a starts with a "z" (pattern matching).
if [[ $a == "z*" ]]; then # True if $a is equal to z* (literal matching).
if [ $a == z* ]; then # File globbing and word splitting take place.
if [ "$a" == "z*" ]; then # True if $a is equal to z* (literal matching).
I hope that helps!
Solution 3
It seems that you are looking to parse commandline arguments into your bash script. I have searched for this recently myself. I came across the following which I think will assist you in parsing the arguments:
http://rsalveti.wordpress.com/2007/04/03/bash-parsing-arguments-with-getopts/
I added the snippet below as a tl;dr
#using : after a switch variable means it requires some input (ie, t: requires something after t to validate while h requires nothing.
while getopts “ht:r:p:v” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
t)
TEST=$OPTARG
;;
r)
SERVER=$OPTARG
;;
p)
PASSWD=$OPTARG
;;
v)
VERBOSE=1
;;
?)
usage
exit
;;
esac
done
if [[ -z $TEST ]] || [[ -z $SERVER ]] || [[ -z $PASSWD ]]
then
usage
exit 1
fi
./script.sh -t test -r server -p password -v
Related videos on Youtube
Alejandro
Updated on July 08, 2022Comments
-
Alejandro almost 2 years
I am doing some bash script and now I got one variable call
source
and one array calledsamples
, like this:source='country' samples=(US Canada Mexico...)
as I want to expand the number of sources (and each source has its own samples) I tried to add some arguments to do this. I tried this:
source="" samples=("") if [ $1="country" ]; then source="country" samples="US Canada Mexico..." else echo "try again" fi
but when I ran my script
source countries.sh country
it didn't work. What am I doing wrong?-
evil otto about 12 yearshow does it not work? When you run it you get an error, or it says "try again", or something else?
-
shellter about 12 yearsyes, you may think 'the question is very straightforward'. but you've been thinking about this for a while. Please consider editing your question to include required outputs and any error messages you're getting. +1 for actually trying something and good formatting of question. good luck.
-
Stef about 8 yearsWhy is this code the same as the proposed and accepted solution? That's confusing... I'm guessing the initial question didn't have the spaces right after the opening bracket and before the closing bracket of the if sentence?
-
Will over 6 years@Stef if you look closely,
$1="country"
vs.$1 = "country"
-
-
Alejandro about 12 yearsI cannot believe that I expend three hours on this, and was only a problem of spaces!!!!... THANKS @Alex
-
user unknown about 12 yearsWhy do you use
samples="US Canada Mexico..."
after introducing the array creation beforesamples=(US Canada Mexico...)
? -
andrewb about 9 yearsI ran into issues with this when the variable on the left was an empty string. Fix was
if [ "$1" = "country" ]; then
. -
omni about 8 yearsOkay thats it. Bash officially gets my vote for beeing the most outdated yet still used command language. Syntax is just so non intuitive it hurts. Really no place for it in 2016+. @Systemd authors: can you please make "building a Linux shell that doesn't suck" your next project? I'll throw money at you.
-
unsynchronized almost 8 years@masi sometimes i think the original intent of unix was "job security through obscurity". if the tools were cryptic to use, hard to understand, and generally out of reach of the great unwashed without the inside knowledge handed down from master to padwan, it would ensure that there would always be work for those "in the know". this philosophy is evident in the whole "RFC" construct, and flowery language used in man documents, which whilst technically telling you how to use the tools, really just were there to fulfill a requirement of having documented the tools. little has changed.
-
Erwan Clügairtz almost 7 yearsDidn't work for me, I had to put $1 between quotes if [ "$1" != "unik" ];
-
Nick T over 5 yearsIf scripts are at all 'logic-heavy' (>1
if
, >0while
...) I strongly try to use Python.subprocess.run
andos.exec*
can go pretty far. -
Jethro almost 4 yearsIn addition to the
"$1"
fix mentioned by others, you can also consider using the more modern (if bash only)if [[ $1 = "country" ]]; then
, which handles empty values but also gives various other features. -
3VYZkz7t about 3 yearsYear 2021 update: The answer doesn't work as is with bash 5.0.17.
if [[ $1 = "country" ]]; then
works.