I am working on a shell script where it has to extract string from parameter passed. If I am passing parameters like
test.sh arg1=someArgument Arg2=AnoTherArgument
Assume user may pass the arguments with the name and value in any case,
I have to extract the parameters and manipulate, some thing like
for arg in "$#"
do
if [ lower("${arg}") == "arg1" ] then
# extract arg1's value and do something
elif [ lower("${arg}") == "arg2" ] then
# extract arg2's value and do something
fi
done
Please help me how can I extract the parameter's value in the same case that was passed?
For clarity, I prefer a case statement like this:
while [ $# -gt 0 ]
do
case $1 in
a=* ) echo ${1#a=};;
b=* ) echo ${1#b=};;
# etc
esac
shift
done
Use double bracket to match glob pattern ; semicolon is needed before "then"
if [[ "$arg" = a=* ]];then echo ${arg#a=};fi
or
if [ "${arg%%=*}" = a ];then echo ${arg#a=};fi
for more information
man bash
/Parameter Expansion
Why bother working to parse arguments? Pass things through the environment:
In foo:
#!/bin/sh
echo Value of arg1: ${arg1-default value}
Then:
$ arg1=blah ./foo
Value of arg1: blah
and
$ ./foo
Value of arg1: default value
Related
I am trying to create an if statement using the test command that checks if the variable "name" contains "Scott Pearce".
#!/bin/bash
name="Scott Pearce"
if test $name == "Scott Pearce";
then
echo "Yes"
else
echo "No"
fi
When I run the script I get an error saying :
./script1: line 5: test: too many arguments
Any idea what I am doing wrong?
if test "$name" = "Scott Pearce";
You need to quote the variable, otherwise when the shell expands it, test will not get the right number of arguments since your variable contains a space.
Also the string equality operator for test is =, not ==.
If you really want to test contains, then pick one of
bash
if [[ $name == *"Scott Pearce"* ]]; then ...
POSIX
case "$name" in
*"Scott Pearce"*) echo Hi Scott ;;
*) echo Begone stranger ;;
esac
The == operator in bash's [[ command is a pattern matching operator.
I wanted to see if there is a parameter provided. But i don't know why this code wont work. What am i doing wrong?
if ![ -z "$1" ]
then
echo "$1"
fi
Let's ask shellcheck:
In file line 1:
if ![ -z "$1" ]
^-- SC1035: You need a space here.
In other words:
if ! [ -z "$1" ]
then
echo "$1"
fi
If, as per your comment, it still doesn't work and you happen to be doing this from a function, the function's parameters will mask the script's parameters, and we have to pass them explicitly:
In file line 8:
call_it
^-- SC2119: Use call_it "$#" if function's $1 should mean script's $1.
You could check the number of given parameters. $# represents the number of parameters - or the length of the array containing the parameters - passed to a script or a function. If it is zero then no parameters have been passed. Here is a good read about positional parameters in general.
$ cat script
#!/usr/bin/env bash
if (( $# == 0 )); then
echo "No parameters provided"
else
echo "Number of parameters is $#"
fi
Result:
$ ./script
No parameters provided
$ ./script first
Number of parameters is 1
$ ./script first second
Number of parameters is 2
If you would like to check the parameters passed to the script with a function then you would have to provide the script parameters to the function:
$ cat script
#!/usr/bin/env bash
_checkParameters()
{
if (( $1 == 0 )); then
echo "No parameters provided"
else
echo "Number of parameters is $1"
fi
}
_checkParameters $#
This will return the same results as in the first example.
Also related: What are the special dollar sign shell variables?
In this script I found this if expression:
if [ -z $1 ]; then
echo "Usage: createpkg.sh <rev package>"
exit
else
CURRENT_VERSION=$1
fi
My problem is that I can't find what exactly means this -z value.
From the content of the echo I can deduct that (maybe) $1 variable represents the sotware version. and that (maybe) -z is a void value. So if I execute the script without passing to it the version of the software that I would packing it print me the correct procedure to execute the script.
But I am not sure about the real meaning of the -z value.
From man test:
-z STRING
the length of STRING is zero
So the condition:
if [ -z $1 ]; then
means "if the variable $1 is empty". Where $1 is probably the first parameter of the script: if you execute it like ./script <parameter1> <parameter2>, then $1=parameter1, $2=parameter2 and so forth.
help test tells:
String operators:
-z STRING True if string is empty.
In your example, the script would print Usage: createpkg.sh <rev package> and exit if an argument was not supplied.
Trying to set rules if a certain variable is put into place, can someone identify wtf I'm missing here?
test.sh:
#!/bin/bash
args=($#)
if [ "$#" = "--cron" ]; then
echo "WORKS!";
else echo "FAILS"
fi
output of ./test.sh:
./test.sh: line 3: [: =: unary operator expected
FAILS
However, when I run ./test.sh --cron, it works, and WORKS is output.
The correct way to do this varies a bit depending on exactly what you're trying to do. If you want to check whether the first argument is --cron, use this:
if [ "$1" = "--cron" ]; then
If you want to check whether the only argument is --cron, use this:
if [ "$*" = "--cron" ]; then
(Note that this is one of very few cases where "$*" is the right way to do something -- it expands to all arguments separated by spaces, but treated as a single word for parsing purposes.)
If you want to check whether any argument is --cron, use this:
cronopt=false
for argument; do
if [ "$argument" = "--cron" ]; then
cronopt=true
break # note: if you are scanning the arguments for other things too, remove this
fi
done
if $cronopt; then
...
BTW, I'm not sure what you're using the args=($#) line for, but if you want to store the arguments in an array the correct way to do it is args=("$#") -- the quotes keep it from doing word splitting, filename expansion, etc before putting the args into the array.
This should work, but only for the first element, of you want more you might have to do a for or while loop to iterate thru the arguments.
#!/bin/bash
args=($1)
if [ $args ] && [ $args = "--cron" ]; then
echo "WORKS!";
else echo "FAILS"
fi
In this case, I believe "$#" is too well quoted.
Try comparing against "$1", or use:
#!/bin/bash
args=($#)
if [ "$*" = "--cron" ]; then
echo "WORKS!";
else
echo "FAILS"
fi
How can a Bourne Shell script know that the first parameter it received was '' (Two single quotation marks?
I've tried
if [ -z "$1" ] ; then
echo "Wrong number of parameters"
fi
But it seems that the $1 expands to an empty string and so is "$1".
When you type '' in command line shell translate it to argument - zero length string.
Check variable that holds the number or arguments (before checking -z "$1").
# check for any arguments
if [ "$#" -eq 0 ]; ...
# or -- has arguments and first one is ''
if [ "$#" -gt 0 -a -z "$1" ]; ...
See 'man test' for INTEGER comparison tests (-eq, -gt, etc).
EDIT (based on comments to question):
On windows (what shell do you use?) you have to check for '' (two characters) (cmd.exe passes it that way I think). On linux your script get an argument of string length zero.
if [ \( "$#" -gt 0 -a -z "$1" \) -o "$1" = "''" ]; ...
I assume what you mean is that a parameter was passed, but its value is empty. This is how to check it:
if [ $# -gt 0 -a "$1" = '' ]
then
echo '$1 was passed, but empty'
fi
If you want to check how many parameters were passed (empty or not), then use $# (argument count):
if [ $# -eq 0 ]
then
echo 'no parameters were passed'
fi
If you want to check the difference between two double quotation marks ("") and single quotation marks (''), there's no way to do that in Bourne shell alone. By the time your code is executed, these strings have been evaluated to the empty string.
'' is obviously not an empty string; it contains two characters. Do
[ "$1" = "''" ]
But then, on the (Linux) command line, you'll have to pass the parameter as
./script.sh "''"
if [ "$1" == "--" ] ; then
echo "Wrong number of parameters"
fi