Bash ARGS help - getting random error - linux

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

Related

how to use $* but it doesnt consider a special arguement in bash

I want to use $* in a loop but the first argument is ignored.
in other words:
sum=0
for i in $*
do
if [ $1 = "+" ]; then
sum=$(($sum+$i))
fi
done
echo $sum
To remove the first parameter, use shift.
But before you shift, you should store the value somewhere.
As the first parameter doesn't change, you can check it just once before starting the loop:
#! /bin/bash
op=$1
shift
if [ "$op" = + ] ; then
sum=0
for i in $* ; do
sum=$(($sum+$i))
done
echo $sum
fi
Note that you don't need the quotes around +. You should quote the $op in the condition, though, to prevent parsing errors (try running the script specifying an empty string "" as the first argument).
When using $((, note that you can use shorter and faster way to increment a variable:
((sum+=i))

how can i add a command to put any value on an argument to echo invalid input

i have to keep the yes and no choice but i want to put something that if i don't type yes or no to show that this is not possible i am new to this and not sure what to do
echo "Do you wish to Exit?(yes/no)"
read input
if [ "$input" == "yes" ]
then
clear
exit
elif [ "$input" == "no" ]
then
clear
echo "Reloaded"
echo -e "\n"
elif [ "$input" == * ]
then
echo "Invalid Input"
fi ;;
You are on the right track here, the last condition check is causing issues however.
You are checking if "$input" == * in the last block. When you use * on its own like that you can get some wacky behavior. The shell tries to expand it to all the files in the current directory. This means that you will likely be giving too many arguments to the conditional and should get an error similar to [: too many argument when there are several files in the current directory. If the directory is empty except for the given script the conditional will be expanded to elif [ "$input" == some_file.txt] and the script will continue and exit normally without the desired output. See the bash pattern matching and command expansion documentation.
The simplest solution here is to use an else instead. This block will execute if the first two conditions are not met, and therfore $inputs is something other than yes or no. See the bash conditional documentation. You script should look something like this:
echo "Do you wish to Exit?(yes/no)"
read input
if [ "$input" == "yes" ]
then
clear
exit
elif [ "$input" == "no" ]
then
clear
echo "Reloaded"
echo -e "\n"
else
echo "Invalid Input"
fi
As a final comment, you can simplify the read command into 1 line by leveraging the -p argument, from the usage:
-p prompt output the string PROMPT without a trailing newline before attempting to read
So you can condense your read into read -p 'Do you wish to Exit? (yes/no)' input

How to check if there is a parameter provided in bash?

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?

Delimiter “, white spaces and bash script in Linux

I want in a bash script (Linux) to check, if two files are identical.
I use the following code:
#!/bin/bash
…
…
differ=$(diff $FILENAME.out_ok $FILENAME.out)
echo "******************"
echo $differ
echo "******************"
if [ $differ=="" ]
then
echo "pass"
else
echo "Error ! different output"
echo $differ
fi
The problem:
the diff command return white space and break the if command
output
******************
82c82 < ---------------------- --- > ---------------------
******************
./test.sh: line 32: [: too many arguments
Error ! different output
The correct tool for checking whether two files are identical is cmp.
if cmp -s $FILENAME.out_ok $FILENAME.out
then : They are the same
else : They are different
fi
Or, in this context:
if cmp -s $FILENAME.out_ok $FILENAME.out
then
echo "pass"
else
echo "Error ! different output"
diff $FILENAME.out_ok $FILENAME.out
fi
If you want to use the diff program, then double quote your variable (and use spaces around the arguments to the [ command):
if [ -z "$differ" ]
then
echo "pass"
else
echo "Error ! different output"
echo "$differ"
fi
Note that you need to double quote the variable when you echo it to ensure that newlines etc are preserved in the output; if you don't, everything is mushed onto a single line.
Or use the [[ test:
if [[ "$differ" == "" ]]
then
echo "pass"
else
echo "Error ! different output"
echo "$differ"
fi
Here, the quotes are not strictly necessary around the variable in the condition, but old school shell scripters like me would put them there automatically and harmlessly. Roughly, if the variable might contain spaces and the spaces matter, it should be double quoted. I don't see a need to learn a special case for the [[ command when it works fine with double quotes too.
Instead of:
if [ $differ=="" ]
Use:
if [[ $differ == "" ]]
Better to use modern [[ and ]] instead of an external program /bin/[
Also use diff -b to compare 2 files while ignoring white spaces
#anubhava answer is correct,
you can also use
if [ "$differ" == "" ]

shell script to extract a string from parameter

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

Resources