Bash function always ignores the else block - bash

When I run the below code with exact two arguments, the else block doesn't get executed.
If I take out the if else block out of the function, everything works fine.
#!/bin/bash
usage() {
if [[ $# -gt 2 || $# -lt 2 ]]; then
echo "insufficient args"
else
if [[ $# -eq 2 ]]; then
echo "continuing with the script"
fi
fi
}
usage

In this situation, the function usage is receiving 0 arguments from the call.
Change the call to usage $#, which will pass the command line arguments to the usage function.
#!/bin/bash
usage() {
if [[ $# -gt 2 || $# -lt 2 ]]; then
echo "insufficient args"
else
if [[ $# -eq 2 ]]; then
echo "continuing with the script"
fi
fi
}
usage "$#"

Related

Check parameters of script in bash

I wanna write a script which check variables of this script.
I have tried some, but it isn't working. The idea is:
If on of the parameters is a number, print that it is number
If on of the parameters is a character, print that it is character
If 'man parameter' is executable, print that it is might be a function
Script I have tried:
#!/bin/bash
echo Hello $LOGNAME'!'
test $# -eq 0 && echo 'Try again, no parameters were entered' || echo 'Num of parameters is '$#
re='^[0-9]+$'
for i in $*
do
if ![["$i" =~ $re]];then
echo 'Parameter '$i' is alphabetical'
else
if [["$i" =~ $re]];then
echo 'Parameter '$i' is digital'
else
if [ $i];then
echo $i' might be a function. Try to use man of --help'
fi
fi
fi
done
#!/bin/bash
echo "Hello ${LOGNAME}!"
[ "$#" -eq 0 ] && { echo 'Try again, no parameters were entered'; exit 1; }
echo 'Num of parameters is '$#
re='^[0-9]+$'
for i in "$#"
do
if ! [[ "$i" =~ $re ]];then
echo "Parameter '$i' is alphabetical"
man "$i" > /dev/null 2> /dev/null
if [ "$?" -eq 0 ];then
echo "$i might be a function. Try to use man of --help"
fi
else
echo "Parameter '$i' is digital"
fi
done;
When you write a test you need spaces around your brackets.
You can easily find and fix those bugs if you use shellcheck

if condition inside function is not working as desired when function called with command line arguments inside find statement

#!/bin/bash
# Code to generate script usage
if [[ "$#" -ne 1 ]] && [[ "$#" -ne 2 ]]; then
flag=1;
elif ! [[ "$1" == "abcd" || "$1" == "dcba" ]]; then
echo "Invalid"
flag=1;
fi
while [ $# -gt 1 ]
do
case $2 in
'streams')
;;
*)
echo "unrecognised optional arg $2"; flag=1;
;;
esac
shift
done
if [ "$flag" == "1" ]; then
echo "Usage:"
exit
fi
function main {
arg1=$1
streams=$2
if [ "${streams}" == "streams" ]; then
echo entering here
else
echo entering there
fi
}
parent_dir=`pwd`
find $parent_dir -name "*" -type d | while read d; do
cd $denter code here
main $1 $2
done
Why the code does not enter "entering here" when script run with arguments "abcd" and "streams" ?
I feel that function having two arguments is causing the problem, code was working fine with one argument
Several things you might want to fix in your code, before attempts are made to find the specific problem. It is possible that it will disappear after modifying your script accordingly. If the problem is still alive, I'll edit my answer with a solution. If you decide to apply the following changes, please update your code in the question.
Consistent usage of either [[ or [. [[ is a Bash keyword similar to (but more powerful than) the [ command.
See
Bash FAQ 31
Tests And Conditionals
Unless you're writing for POSIX sh, I recommend [[.
Use (( for arithmetic expressions. ((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for let, if assignments are needed. See Arithmetic Expression.
Use the variable PWD instead of pwd. PWD is a builtin variable in all POSIX shells that contains the current working directory. pwd(1) is a POSIX utility that prints the name of the current working directory to stdout. Unless you're writing for some non-POSIX system, there is no reason to waste time executing pwd(1) rather than just using PWD.
The function keyword is not portable. I suggest you to avoid using it and simply write function_name() { your code here; } # Usage
$parent_dir is not double-quoted. "Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[#]}", "a & b". See
Quotes
Arguments
ShellCheck your code before uploading.
Replace the while condition logic with an if condition, so that shift is no longer required. Shift was the devil I was facing I found.
#!/bin/bash
# Code to generate script usage
if [[ "$#" -ne 1 ]] && [[ "$#" -ne 2 ]]; then
flag=1;
elif ! [[ "$1" == "abcd" || "$1" == "dcba" ]]; then
echo "Invalid"
flag=1;
fi
#while [[ $# -gt 1 ]]
#do
# case $2 in
# 'streams')
# ;;
# *)
# echo "unrecognised optional arg $2"; flag=1;
# ;;
# esac
# shift
#done
if [[ $2 == "streams" ]]; then
:
elif [[ (-z $2) ]]; then
:
else
echo "unrecognised optional arg $2"; flag=1;
fi
if [[ "$flag" == "1" ]]; then
echo "Usage:"
exit
fi
function main {
streams=$2
if [[ "${streams}" == "streams" ]]; then
echo entering here
else
echo entering there
fi
}
parent_dir=`pwd`
find $parent_dir -name "*" -type d | while read d; do
cd $d
main $1 $2
done

Shell script avoiding that 2 options can be used at the same time

I'm writing a shell script
it works great, the only problem I have is that I want to avoid the possibility of using both options -d) and -x) at the same time when executing my command with my parameters in directories.
Could this be possible with a minimal change in my code?
#!/bin/bash
dir=$1
if [ $# -lt 1 ] ; then
echo "ERROR: no argument"
exit 1 # pas 0
else
case $2
in
-d)
mv $dir/ /tmp/
echo 'moving with -d'
;;
-x)
for f in "$dir"/*; do [[ -x $f ]] && mv "$f" /tmp; done
echo 'moving executables'
;;
*)
mv $dir/* /tmp/
echo 'no flag passed so moving all'
echo "mv $dir/* /tmp/"
;;
esac
fi
I would do it the other way: first extract options, then "if" it.
#!/bin/bash
dir=$1
shift
while [ $# -gt 0 ] ; do
case $1
in
-d)
D_OPTION_SELECTED=1
;;
-x)
X_OPTION_SELECTED=1
;;
esac
shift
done
help() {
echo "Usage $0 dir [-x or -d]";
}
if [[ "$dir" == "" ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 && $X_OPTION_SELECTED -gt 0 ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 ]]; then echo D selected; fi
if [[ $X_OPTION_SELECTED -gt 0 ]]; then echo X selected; fi
But please remember that the good rule is to allow options at first places. So the better version would be:
#!/bin/bash
while [ $# -gt 0 ] ; do
case $1
in
-d)
D_OPTION_SELECTED=1
;;
-x)
X_OPTION_SELECTED=1
;;
*)
dir=$1
;;
esac
shift
done
help() {
echo "Usage $0 [-x or -d] dir";
}
if [[ "$dir" == "" ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 && $X_OPTION_SELECTED -gt 0 ]]; then help; exit 1; fi
if [[ $D_OPTION_SELECTED -gt 0 ]]; then echo D selected; fi
if [[ $X_OPTION_SELECTED -gt 0 ]]; then echo X selected; fi
echo dir=$dir

Argument checking in bash

I am trying to check the arguments that are passed into the script. It should have a minimum of 2 arguments and can have a maximum of 3. The 3rd argument if present should be "-I". I though I could do this but its not working.
if [ \( ! $# = 2 \) -o \( $# = 3 -a "$3" != "-I" \) ];then
exit 0
fi
What am I doing wrong? Any suggestions on how to make it work?
In bash, you can do something like this:
#!/bin/bash
if [[ $# -eq 3 ]] ; then
if "$3" != "-I ]] ; then
echo "Argument 3 must be '-I' if present"
exit
fi
fi
if [[ $# -ne 2 && $# -ne 3 ]] ; then
echo "Needs two or three arguments"
exit
fi
echo "[$1]"
echo "[$2]"
echo "[$3]"
Try this
#!/bin/bash
MAX_ARGUMENTS=3
echo $#
if [ $# -eq $MAX_ARGUMENTS ]
then
echo "hi"
last=${!#}
if [ $last == "-l" ]
then
echo "its l"
else
echo "its not l"
fi
else
echo "bye"
fi

How to count and check passed arguments?

How can I translate the following Ruby code to Bash?
if ARGV.length == 0
abort "\nError: The project name is required. Aborting...\n\n"
elsif ARGV.length > 2
abort "\nError: The program takes two arguments maximum. Aborting...\n\n"
end
#!/bin/bash
USAGE="$0: <project name> [subproject attribute]"
if [ $# -lt 1 ]; then echo -e "Error: The project name is required.\n$USAGE" >&2; exit 1; fi
if [ $# -gt 2 ]; then echo -e "Error: Two arguments maximum.\n$USAGE" >&2; exit 1; fi
The following should be what you need:
#!/bin/bash
if [ $# -eq 0 ]; then
echo -e "\nError: The project name is required. Aborting...\n\n"
exit 1
elif [ $# -gt 2 ]; then
echo -e "\nError: The program takes two arguments maximum. Aborting...\n\n"
exit 1
fi
The TLDP bash guide is very good if you are looking to learn bash, see TDLP Bash guide.
Maybe:
#!/bin/bash
function functionName {
if [ $# = 0 ]
then echo "\nError: The project name is required. Aborting...\n\n"; exit 1
fi
if [ $# \> 2 ]
then echo "\nError: The program takes two arguments maximum. Aborting...\n\n"; exit 1
fi
}
functionName a

Resources