How can i check if no arguments are given [duplicate] - bash

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

Related

bash if statement command not found [duplicate]

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

How to extract value from a string in shell script? [duplicate]

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 8 months ago.
I have this environment variable
echo $DT_CUSTOM_PROP
returns
APPCODE=IK22 ENVIRONMENT=DEV APPLICATION=xxxxx-xxxxx-xxxxx-xxxxx ANOTHERKEY=ANOTHERVALUE
How can I get APPLICATION from it?
So that if I do echo $APPLICATION I get xxxxx-xxxxx-xxxxx-xxxxx
Given:
DT_CUSTOM_PROP='APPCODE=IK22 ENVIRONMENT=DEV APPLICATION=xxxxx-xxxxx-xxxxx-xxxxx ANOTHERKEY=ANOTHERVALUE'
You can do it with parameter expansion in a 2-step process:
tmp=${DT_CUSTOM_PROP#*APPLICATION=}
application=${tmp%% *} # ==> xxxxx-xxxxx-xxxxx-xxxxx
Or with bash regex matching
if [[ $DT_CUSTOM_PROP =~ "APPLICATION="([^[:blank:]]+) ]]; then
application=${BASH_REMATCH[1]}
else
application="not found"
fi

what is the -n operator in bash shell script and stand for? [duplicate]

This question already has answers here:
What does -n mean in Bash?
(2 answers)
Closed 3 years ago.
I found this -n operator in bash shell script. I don't have any clue about this and searched, but didn't find any helpful resource.
This is the script code:
#!/bin/bash
while [ -n "$1" ] ....
I also tried to get the output of the -n like:
#!/bin/bash
echo -n #return nothing
Your help will be appreciated!
-n string returns true if the length of the string is non-zero.
This is documented in Bash Conditional Expressions

Trying to understand a Bash script given to me, specifically the while loop [duplicate]

This question already has answers here:
What is "-le" in shell script?
(2 answers)
Closed 4 years ago.
I am using a script made by one of my former colleagues, he told me I'll need some working with it. I am wondering what this while loop does:
# This is the loop that does the simulation
lastsim=0
nextsim=`/usr/bin/expr $lastsim + 1`
while [ $nextsim -le $upperlimit ]
do
cp -i Dynamics_${lastsim}_500ps/*.prmtop ./$paramInput.prmtop
specifically I'm confused by thie -le syntax
This is only part of the script I can upload the rest if necessary.
-le means less or equal to. See the following example which would print 0-9:
i=0
while [ $i -le 9 ]; do
echo $i
let i++
done

bash sh script always executes regardless of parameter [duplicate]

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

Resources