How to check for the existence of a second argument [duplicate] - bash

This question already has answers here:
How do I find the number of arguments passed to a Bash script?
(4 answers)
Check existence of input argument in a Bash shell script
(12 answers)
Closed 7 years ago.
I need to update this bash function I use for doing things with git:
push() {
a=$1
if [ $# -eq 0 ]
then
a=$(timestamp)
fi
# ... do stuff
}
but I don't know how this line works
if [ $# -eq 0 ]
I need to check for a first argument and then I need to check for a second argument.
So there will be 2 if statements.
How can I update this and how does this line work
if [ $# -eq 0 ]

The $# part is a variable that contains the number of arguments passed to the script.
The conditional statement there checks the value of that variable using -eq and it's checking if the value is zero (as in no arguments were passed).
In order to check for two arguments, you can change (or add) that line to read like this:
if [ $# -eq 2 ]

You could create a small script to look into how $# changes when you call the function with different numbers of arguments. For instance:
[Contents of "push.sh":]
push() {
echo $#
}
echo "First call, no arguments:"
push
echo "Second call, one argument:"
push "First argument"
echo "Third call, two arguments:"
push "First argument" "And another one"
If you put this in a script and run it, you'll see something like:
-> % ./push.sh
First call, no arguments:
0
Second call, one argument:
1
Third call, two arguments:
2
This tells you that the value of $# contains the number of arguments given to the function.
The if [ $# -eq 0 ] part you can add to the script, and change the 0 to some other numbers to see what happens. Also, an internet search for "bash if" will reveal the meaning of the -eq part, and show that you could also use -lt or -gt, for instance, testing whether a number is less than or greater than another.
In the end, you'll likely want to use something like the following:
a=$1
b=$2
if [ $# -lt 1 ]
then
a=$(timestamp)
fi
if [ $# -lt 2 ]
then
b=$(second thing)
fi

Related

Explain Bash Script

Can someone please help me to understand this bash script? I am really struggling with first block and last block specifically if and then blocks of the code.
if [[ $# -ne 2 ]] ; then
echo 'Cluster name, Azure Subscription ID required as input arguments.'
echo 'Example usage: ./create_blob_container.sh <CLUSTER_NAME> <ARM_SUBSCRIPTION_ID>'
exit 1
fi
MAX_CHAR_CLUSTER_NAME=8
CLUSTER_NAME=${1}
ARM_SUBSCRIPTION_ID=${2}
ARM_STORAGE_ACCOUNT_TFSTATE_NAME=${CLUSTER_NAME}tfstate$(echo ${ARM_SUBSCRIPTION_ID}|cut -c1-
8)
RESOURCE_GROUP_NAME=stack-tfstate-rg
CONTAINER_NAME=tfstate
LOCATION=westeurope
SKU=Standard_LRS
ENCRYPTION_SERVICES=blob
if [ ${#CLUSTER_NAME} -gt ${MAX_CHAR_CLUSTER_NAME} ]; then
echo "ERROR: Project name is too long (${#CLUSTER_NAME} chars). Maximal number of allowed
chars is ${MAX_CHAR_CLUSTER_NAME}. Exiting..."
exit 1`enter code here`
fi
Thank you
The first block means if the number of the positional parametre passed to the script is not equals to 2 then the script will execute 2 echo and exit.
if [[ $# -ne 2 ]]
Here a link that might help you.
The last block means if the length of the string stored in the variable CLUSTER_NAME which is the first position parametre is greather than MAX_CHAR_CLUSTER_NAME which is 8
if [ ${#CLUSTER_NAME} -gt ${MAX_CHAR_CLUSTER_NAME} ]; then
Another link

Simple bash shell script to ensure that provides two arguments

I'm trying to write simple greetings script in bash to ensure that the user provides at least two arguments. 1)The first argument provides how long we want to delay before the greeting prints. 2) The second argument provides what message we want to display. Sorry I'm fairly new to shell scripting.
#!/bin/sh
if[$# -ge 2]
then
sleep $1
shift
banner $*
else
echo "Usage: Greeing seconds word(s)"
fi
You should use:
#!/bin/sh
if [ $# -ge 2 ]
then
sleep $1
shift
banner $*
else
echo "Usage: Greeing seconds word(s)"
fi
You were missing:
a space after if
a space around [ and ]
If you want to ensure that 2 arguments are given, use ${2?}. For example:
echo First arg is ${1?Missing first argument}. 2nd arg is ${2?Missing 2nd argument}

Determining if there is a next argument while iterating through the arguments

While iterating through the arguments, how do you determine if there is a next argument?
The way I tried to approach this was to check if the next argument is not empty but I ran into some problems.
Here in this example I print the value of the current argument and if there is an argument that comes after that then print some message.
My approach:
use $i+1 where $i+1 will give you the value of the next index.
#!/bin/sh
for i in "$#"
do
echo $i
if ! [ ${i+1}="" ]; then
echo "test"
fi
done
sh test 1 2 3 4 5
but that didn't work. I also tried expr i + 1, but that didn't work as well.
If anyone could give me a hint on how to approach this problem that would be really appreciated.
#!/bin/sh
while [ $# -gt 0 ] ; do
echo $1
if [ -n "${2+x}" ]; then
echo another arg follows
fi
shift
done
$ ./test.sh 1 2 3
1
another arg follows
2
another arg follows
3
The trick here is that we use shift for consuming the argument list instead of iterating over it. The next argument is always $1, which we know exists because we only execute the loop if $# (the count of the positional arguments, not including $0) is positive. To check whether the argument after that, $2, exist, we can use the ${PARAM+WORD} expansion, which produces nothing if PARAM doesn't exist, otherwise produces WORD.
Of course, shift destroys the argument list. If you don't want that, move things into a function. The following example shows how we can process the same argument list twice by passing a copy into a function in which shift locally eats it:
#!/bin/sh
func() {
while [ $# -gt 0 ] ; do
echo $1
if [ -n "${2+x}" ]; then
echo another arg follows
fi
shift
done
}
func "$#"
func "$#"
$ ./test.sh 1 2 3
1
another arg follows
2
another arg follows
3
1
another arg follows
2
another arg follows
3
You can use a counter and check for $#:
n=1
for i in "$#"; do
echo "$i"
if [ $n -eq $# ]; then
echo "test"
fi
n=$(expr $n + 1)
done

Argument check- bash

Suppose I have a script that takes two arguments, is there way to check if there is one argument or 2+ arguments in say the form:
if [ check if arguments don't equal 2 ]; then
echo Too many arguments
exit 1
fi
if [ $# -ne 2 ]; then
# Number of arguments was not 2
fi
The variable $# holds the number of passed arguments.

Argument Checking Problem in Bash Script

So basically I am trying to check the arguments that are passed into the script. If it has three arguments and the third argument is a 1, then I want it to continue. I also want it to continue if it has four arguments and the third argument is not a 1.
So basically I thought that I could just do...
if ([ $# -ne 3 ] and [ "$3" -ne "2" ])
then
exit 0
fi
However it seems that Bash does not have and's to use for if's,so then I figured that I could just use nested if's, however now it's complaining still. So this is what I have currently...
if [ $# -ne 3 ]
then
if [ "$3" -ne "1" ]
then
echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>
Select can be left off if you want all data (Mode=1)
"
exit 0
fi
fi
if [ $# -ne 4 ]
then
if [ "$3" -ne "2" ]
then
echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>
Select can be left off if you want all data (Mode=1)
"
exit 0
fi
fi
So where am I going wrong? Can I not nest if statements in Bash? Is there a super-zen way of doing this that I'm missing altogether?
Thanks for the any help you could give me.
New Problem...
Now, for some reason or another, the code isn't working at all. There are no errors or anything, it just doesn't work. It doesn't check the number of arguments. I've run the script with no arguments at all and it just skips it like it's not even there.
Weird part is that I was sure that the code was working yesterday. Come back today, not so much. Any ideas on what the problem is? (Sorry, but I have to remove the accepted answer on this.)
if [[ $# = 3 && "$3" != "1" ]]
then
echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>
Select can be omitted if all data is required (Mode=1)
"
exit 0
fi
if [[ $# > 4 ]]
then
echo "Improper number of arguments.
FORMAT make-csv-data <STUDY> <TAG> <MODE> <SELECT>
Select can be omitted if all data is required (Mode=1)
"
exit 0
fi
EDIT II:
There are a few things that the Bash shell isn't liking about this script that I'm trying to do. I'll probably end up rewriting it in another scripting language and do a few more things that I have in mind for the project. Thanks for the help in any case.
if [ $# -ne 3 -a "$3" -ne "1" ]; then
exit 0
fi
For reference
-a = and
-o = or
Or, you could just use use:
if [[ $# != 3 && "$3" != "1" ]]; then
Please see:
http://bash-hackers.org/wiki/doku.php/commands/classictest#and_and_or
and
http://bash-hackers.org/wiki/doku.php/syntax/ccmd/conditional_expression
Since you're just checking exit/return values with "if", you need to provide something, e.g. a command, that provides meaningful ones based on your tests. [ is such a command, another possibility is the [[ keyword.
The actual correct examples already were mentioned by scragar above, I don't want to just repeat them :)

Resources