In this example I want it to know if $# contains two words/symbols "load" and "/"
for one word/symbol this works
case "$#" in */*)
;;
echo "going to do stuff"
*)
echo "will do something else"
;;
esac
or
string='My string';
if [[ "$string" == *My* ]]
then
echo "It's there!";
fi
But if two words/symbols appear at random places I cant figure out how to do it.
Update:
The input will the module command. In this case I want to know if it is the module load with or without / that indicate version. the command will look like this
1) module load appname/1.1.1 or
2) module load appname
3) module (not load) (list, avail etc)
It is number 1 I am interested in for now.
3 will in some cases be variation of 1.
2 will be run as is but will include a message to the user
The slow way would be to iterate through the array twice and then check if both "load" and "/" were present, like this:
for element in $#; do [[ "$element" == "load" ]] && loadPresent=1; done
for element in $#; do [[ "$element" =~ ".*/.*" ]] && slashPresent=1; done
if [[ $loadPresent == 1 ]] && [[ $slashPresent == 1 ]]; then
echo "Contains load and /"
fi
(As I interpreted your question you want one parameter to be exactly "load" and another one to contain a slash.)
Something like this is possible:
if [[ ${#} =~ .*/.* && ${#} =~ ((^)|([ ]))load(($)|([ ])) ]]
then
echo both
fi
-or-
if LOAD=0 && SLASH=0 && \
for ARG in ${#};
do
if [ "${ARG#*/}" != "${ARG}" ]; then SLASH=1; fi
if [ "${ARG}" = "load" ]; then LOAD=1; fi
done && [ "${LOAD}${SLASH}" = "11" ];
then
echo both
fi
-or-
function loadslash()
{
LOAD=0 && SLASH=0
for ARG in ${#};
do
if [ "${ARG#*/}" != "${ARG}" ]; then SLASH=1; fi
if [ "${ARG}" = "load" ]; then LOAD=1; fi
done
test "${LOAD}${SLASH}" = "11"
}
if loadslash ${#}
then
echo both
fi
this will satisfy your requirements
if [[ $1 == "load" ]]; then
if [[ $2 == */* ]]; then
do first case
else
do second case
fi
else
do third case
fi
Related
I'd like to write a script that takes 2 parameters,
The first is parameter that could be "alpha/beta/tcp_friendliness/fast_convergence".
The Second should be a number for the alpha/beta cases and a 0/1 for the other 2.
for example: ./manage_cc alpha 512
Now i've wrote the following script that supposedly covers my cases, but it seems to go into all the conditionals. surely my syntax is broken so any help would be appreciated.
#!/bin/bash
echo -n $2 > /sys/module/tcp_tuner/parameters/$1
if [["$1" == ""] || ["$2" == ""]]
then
echo "You need to pass a property to modify as a first parameter and a value as the second"
fi
if [["$1" == "alpha"] || ["$1" == "beta"]]
then
echo -n $2 > /sys/module/tcp_tuner/parameters/$1
else
if [["$1" == "tcp_friendliness"] || ["$1" == "fast_convergence"]]
then
if [["$2" != "0"] && ["$2" != "1"]]
then
echo "This parameter only accepts a boolean value (0/1)"
exit 1
else
echo -n $2 > /sys/module/tcp_tuner/parameters/$1
fi
else
echo "The only accepted values for first parameter are alpha/beta/tcp_friendliness/fast_convergence"
exit 1
fi
fi
A rewrite of your code:
#!/usr/bin/env bash
write() {
printf "%s" "$2" > "/sys/module/tcp_tuner/parameters/$1"
}
die() {
echo "$*" >&2
exit 1
}
main() {
[[ -z $2 ]] && die "You need to pass a property to modify as a first parameter and a value as the second"
case $1 in
alpha|beta)
write "$1" "$2"
;;
tcp_friendliness|fast_convergence)
if [[ "$2" == "0" || "$2" == "1" ]]; then
write "$1" "$2"
else
die "This parameter only accepts a boolean value (0/1)"
fi
;;
*) die "The only accepted values for first parameter are alpha/beta/tcp_friendliness/fast_convergence"
;;
esac
}
main "$#"
Your condition syntax is wrong. When you start a condition with [[ you have to end it with ]], not just ]. They don't nest like parentheses.
if [[ "$1" == "alpha" || "$1" == "beta" ]]
I've the below code:
#!/bin/bash
. ~/.bash_profile
dt=$(date +"%Y.%m.%d")
if [[ "$#" -eq '0' ]] || [[ "$#" -eq '1' ]]
then
echo -e "Not correct usage"
exit 1
fi
tar zvxf $SHARE/*.gz -C $SHARE/landing/
sleep 3
ops () {
//
some sets of command
//
}
while read -r line
do
if [[ "$1" == "A" ]] || [[ "$1" == "B" ]] || [[ "$1" == "C" ]] && [[ "$2" =~ "$line" ]] || [[ "$2" == "ALL" ]]
then
ops
fi
done < $SHARE/landing/dir/ApprovedList."$1"
When i run the script, i can see that it's able to untar the .gz file and place it into the folder :$SHARE/landing/
But i guess it's not able to read the file and perform the operations inside the ops function, below is the last line of output i'm getting after running the script in interactive mode:
+ read -r line
Any help is most welcome!!!
Please show the input format, the argument format, and some explanation of those. In the meantime, tweaked for readability and error handling - edit to taste. YMMV.
#!/bin/bash
. ~/.bash_profile
ops () { # do some stuff
: some sets of commands ...
}
die() {
printf "%s\n\n Use: $0 x y z ...\n\n" "$1" >&2
kill -term $$ # exit in function behaves like return
}
# dt=$(date +"%Y.%m.%d") # unused
case "$#" in
0|1) die "Insufficient arguments" ;;
esac
tar zvxf "$SHARE"/*.gz -C "$SHARE/landing/" || die "tar failed"
sleep 3
infile="$SHARE/landing/dir/ApprovedList.$1"
[[ -e "$infile" ]] || die "$infile does not exist"
[[ -r "$infile" ]] || die "$infile is nor readable by $0 as $LOGNAME"
while read -r line
do case "$1" in
A|B|C) case "$2" in
*"$line"*|ALL) ops ;;
*) : data does not match, skipping ;;
esac ;;
*) die "Invalid arg1 '$1'";;
esac
done < "$infile"
Notes:
If you require exactly 2 arguments, then let's check exactly that:
die() {
printf "%s\n\n Use: $0 x y\n\n" "$1" >&2
kill -term $$ # exit in function behaves like return
}
and
case "$#" in
2) ;;
*) die "Incorrect # of arguments" ;;
esac
Also, better than the kill - add a trap at the top:
trap 'echo >&2 "ERROR in $0($BASH_SOURCE) at line $LINENO: [$BASH_COMMAND]"; exit $LINENO;' ERR
and use a literal error return in the function.
die() {
printf "%s\n\n Use: $0 x y\n\n" "$1" >&2
return 1
}
The check (except for [[ "$2" =~ "$line" ]])
if [[ "$1" == "A" ]] || [[ "$1" == "B" ]] || [[ "$1" == "C" ]] && [[ "$2" =~ "$line" ]] || [[ "$2" == "ALL" ]]
doesn't change inside the while-loop. So better check once before entering the while-loop.
So now you want to perform ops for each line of $SHARE/landing/dir/ApprovedList."$1".
You can use
xargs -a "$SHARE/landing/dir/ApprovedList.$1" -L 1 ops
EDIT:
When you have a simple check to perform for each line, you can move this check into the ops function.
First save $2 before entering the function:
to_be_checked="$2"
...
ops() {
[[ "$0" =~ "${to_be_checked}" ]] && return
am not expert on bash script and am looking help on. I would like to find a way at the end from the while loop condition that the script can be executed unless one option ([-1|-2|-3]) has been chosen OR none of them.
What the best way to do? I have absolutely no idea how to.
Many Thanks.
#!/bin/bash
echo " $0 [-e <option_e>] [-f <option_f>] [-1|-2|-3] [user#]fqdn"
OPTION1=""
OPTION2=""
OPTION3=""
while (( "$#" )); do
if [ "$1" == "-1" ]; then
OPTION1=1
elif [ "$1" == "-2" ]; then
OPTION2=1
elif [ "$1" == "-3" ]; then
OPTION3=1
fi
shift
done
I forgot to mention. this is a part of the script. further, there are several conditions that can conflict if there more then 2 options chosen.
EDIT: the valid selections either exactly one option or zero, but not two or more!
thank you SamuelKirschner -
I did to add [[ -n $ALL_OPTS ]] && then it works ! I am happy now.
#!/bin/bash
OPTION1=""
OPTION2=""
OPTION3=""
while (( "$#" )); do
if [ "$1" == "-1" ]; then
OPTION1=1
elif [ "$1" == "-2" ]; then
OPTION2=1
elif [ "$1" == "-3" ]; then
OPTION3=1
fi
shift
done
ALL_OPTS="$OPTION1$OPTION2$OPTION3";
echo $ALL_OPTS
if [[ -n $ALL_OPTS ]] && [[ $ALL_OPTS -ge 2 ]];then
echo 'Please provide a maximum of one of the options [-1|-2|-3]' 1>&2
exit 1
fi
You can simply test if all of the OPTION-variables concated have the wrong length with ALL_OPTS="$OPTION1$OPTION2$OPTION3"; test ${#ALL_OPTS} -ge 2 and exit. Where "$OPTION1$OPTION2$OPTION3" is just all of the variables written next to each other, therefor it is "", "1", "11" or "111".
#!/bin/bash
echo " $0 [-e <option_e>] [-f <option_f>] [-1|-2|-3] [user#]fqdn"
OPTION1=""
OPTION2=""
OPTION3=""
while (( "$#" )); do
if [ "$1" == "-1" ]; then
OPTION1=1
elif [ "$1" == "-2" ]; then
OPTION2=1
elif [ "$1" == "-3" ]; then
OPTION3=1
fi
shift
done
ALL_OPTS="$OPTION1$OPTION2$OPTION3";
if test ${#ALL_OPTS} -ge 2; then
echo 'Please provide a maximum of one of the options [-1|-2|-3]' 1>&2
exit 1
fi
From man bash
${#parameter}
Parameter length. The length in characters of the value of parameter is substituted. [...]
Edit: I messed up the condition quite a bit. (original answer was test -z "$OPTION1$OPTION2$OPTION3")
Keeping a count of options might be a possibility. Something like this, using getopts:
#!/usr/bin/env bash
echo "${0##*/} [-e <option_e>] [-f <option_f>] [-1|-2|-3] [user#]fqdn"
count=0
while getopts e:f:123 opt; do
case "$opt" in
e) opt_e="$OPTARG" ;;
f) opt_f="$OPTARG" ;;
1|2|3) declare opt_$opt=true; ((count++)) ;;
esac
done
shift $((OPTIND-1))
# Fail if our counter is too high
((count>1)) && printf 'ERROR: only one digit option allowed.\n' >&2 && exit 1
echo "done"
Alternately, you could keep the numeric options in a variable which you could check using globs:
#!/usr/bin/env bash
echo "${0##*/} [-e <option_e>] [-f <option_f>] [-1|-2|-3] [user#]fqdn"
nopt=""
while getopts e:f:123 opt; do
case "$opt" in
e|f) declare opt_$opt="$OPTARG" ;;
1|2|3) nopt="$nopt$opt" ;;
esac
done
shift $((OPTIND-1))
# Fail if we collected too many digits
((${#nopt}>1)) && printf 'ERROR: only one digit option allowed.\n' >&2 && exit 1
echo "done"
Then, you could switch functionality based on the value of $nopt.
Briefly,
I have a variable ($num) which contains random number(max.18), I need a case statement in shell (because along with checking number, I also have some alphabet conditions) which should validate the user input with the variable (must be less than $num).
Ex:
case $input in
...
1) ... ;;
2) ... ;;
...
so, here if I have only two conditions than I can write code like this, but my variable $num contains random number, how can I write case conditions which satisfies my below requirements.
If user inputs numbers like (1/3,3*1,3-2,2+1) it should not validate as a number
If user inputs numbers like (0001 or 01 or 000001) it should not validate as a number
The case condition should execute only if user inputs number between 1-$num no other number formats or symbols should not allowed.
Ex:
case $input in
[nN*]) ...
[aA*]) ...
...
*) if echo "$input" | egrep '^\-?[0-9]+$'; then
typeset -LZ num
num="$input"
if [ "$input" != "$num" ]; then
echo "$input not a valid number"
fi
else
echo "please choose proper choice option"
fi
;;
This code works but I want a normal case condition which should satisfy my requirements like if we have two or three options we can simply write the code but what if we have random options (which may decrease or increase) how to write a case condition in that case.
Thanks!
If the usage of a case is not compulsory, try and use some regex validation to have more control on what is allowed and what not:
[[ $input =~ ^[1-9][0-9]*$ ]]
# ^ ^ ^ ^
# beginning | | end
# | any digit
# a digit from 1 to 9
This checks that the data in $input contains a number that does not start with 0.
$ r=001
$ [[ $r =~ ^[1-9][0-9]*$ ]] && echo "yes"
$
$ r=1
$ [[ $r =~ ^[1-9][0-9]*$ ]] && echo "yes"
yes
$ r="3+1"
$ [[ $r =~ ^[1-9][0-9]*$ ]] && echo "yes"
$
You can then check if the number is lower than the stored one:
[ $r -le $num ]
All together:
$ num=15
$ r=5
$ [[ $r =~ ^[1-9][0-9]*$ ]] && [ $r -le $num ] && echo "yes"
yes
$ r=19
$ [[ $r =~ ^[1-9][0-9]*$ ]] && [ $r -le $num ] && echo "yes"
$
$ r="3+1"
$ [[ $r =~ ^[1-9][0-9]*$ ]] && [ $r -le $num ] && echo "yes"
$
read -p "enter number" yournumber
re='^[0-9]+$'
if ! [[ $yournumber =~ $re ]] ; then
echo "error: Not a number" >&2; exit 1
fi
This is only code rest of the things you need to do it.
I have just started learning to write bash scripts. This a simplified form of what I want to write.
The problem is despite of the input, it prints only "YES".
#! /usr/bin/bash
read input
if (("$input"== "y" || "$input" == "Y"))
then
echo "YES";
elif (("$input" == "n" || "$input" == "N"))
then
echo "NO";
else
echo "Not a valid input!!"
fi
Use [[ instead of (( like,
if [[ "$input" == "y" || "$input" == "Y" ]]
and also there must be a space exists before == operator.
ie,
input="n"
if [[ "$input" == "y" || "$input" == "Y" ]]
then
echo "YES";
elif [[ "$input" == "n" || "$input" == "N" ]]
then
echo "NO";
else
echo "Not a valid input!!"
fi
You could use regular expression also for condition checking purpose.
if [[ "$input" =~ ^[yY]$ ]]
then
echo "YES";
elif [[ "$input" =~ ^[nN]$ ]]
then
echo "NO";
else
echo "Not a valid input!!"
fi
When you automaticly convert the input to lowercase (using typeset), you do not have to bother with the the uppercases.
When you use an elif, always think 10 seconds for another solution. In this case you might want to use a "switch", in shell written as a case-statement:
#!/usr/bin/bash
typeset -l input
read input
case ${input} in
"y") echo "Yes";;
"n") echo "NO";;
*) echo "Not a valid input!!";;
esac