Linux bash if - elif don't work as expected - linux

My Bash-Script has to do something different, according of the beginning of a Variable.
When I run the following script:
#!/bin/bash
line="__B something"
if [ -n $(echo "$line" | grep "^__A") ]; then
echo "Line __A"
elif [ -n $(echo "$line" | grep "^__B") ]; then
echo "Line __B"
elif [ -n $(echo "$line" | grep "^__C") ]; then
echo "Line __C"
elif [ -n $(echo "$line" | grep "^__D") ]; then
echo "Line __D"
elif [ -n $(echo "$line" | grep "^__E") ]; then
echo "Line __E"
elif [ $(echo "$line" | grep "^__F") ]; then
echo -n "Line __F"
else
echo "something other"
fi
Bash does not recognize that the String start's with __B.:
Output is:
Line __A
What's wrong with my Script?
Thank you for your help!

If you run your script using the xtrace option than you can see what is happening:
bash -x your-script
+ line='__B something'
++ echo '__B something'
++ grep '^__A'
+ '[' -n ']'
+ echo 'Line __A'
Line __A
Because you use the old test built-in (otherwise known as [) the expansion is done but results in nothing to test except the -n. Using the [[ keyword will quote things correctly:
bash -x your-script
+ line='__B something'
++ echo '__B something'
++ grep '^__A'
+ [[ -n '' ]]
++ echo '__B something'
++ grep '^__B'
+ [[ -n __B something ]]
+ echo 'Line __B'
Line __B
However, using the external program grep is a waste. Recent versions of bash have regular expressions (REs) built-in (using the binding operator =~) but you don't need REs in this case, simple globbing will do:
#!/bin/bash
line="__B something"
if [[ $line == __A* ]]; then
echo "Line __A"
elif [[ $line == __B* ]]; then
echo "Line __B"
elif [[ $line == __C* ]]; then
echo "Line __C"
elif [[ $line == __D* ]]; then
echo "Line __D"
elif [[ $line == __E* ]]; then
echo "Line __E"
elif [[ $line == __F* ]]; then
echo -n "Line __F"
else
echo "something other"
fi

I believe it has to do with the subtleties of the difference between using [[ and [ in bash if conditionals.
You can either use [[ and ]] everywhere (seems to work), e.g.
if [[ -n $(echo "$line" | grep "^__A") ]]; then
echo "Line __A"
or you can quote the subshell like this
if [ -n "$(echo '$line' | grep '^__A')" ]; then
echo "Line __A"
[[ has fewer surprises and is generally safer to use. But it is not
portable - Posix doesn't specify what it does and only some shells
support it (beside bash, i heard ksh supports it too). For example,
you can do
[[ -e $b ]]
to test whether a file exists. But with [, you have to quote $b,
because it splits the argument and expands things like "a*" (where [[
takes it literally). That has also to do with how [ can be an external
program and receives its argument just normally like every other
program (although it can also be a builtin, but then it still has not
this special handling).
according to this answer here on stackoverflow. So in your case, most likely bash is separating your string on a space and that is having some side effects.

Related

shell script to print only alpha numeric string and ignore all integers

I am novice to linux scripting. For the below example, i need to split the string as per "-" and store the output in an array as a separate element.
Later, i need to validate each element in an array if its an integer or alphanumeric. if its integer, i need to ignore that element and print only non-integer elements. The following script which i am trying is not giving expected output which should be like 'grub2-systemd-sleep-plugin'.
item = grub2-systemd-sleep-plugin-2.02-153.1
IFS='-'
read -rasplitIFS<<< "$item"
for word in "${splitIFS[#]}"; do echo $word; done
Taking a stab at this here...
Depends on how your numbers may be defined, but I believe you could use something like this to removing numbers from the output. I'm not sure if there is a more efficient way to achieve this
for word in ${splitIFS[#]}
do
c=$(echo $word | grep -c -E "^[0-9]+\.{0,}[0-9]+$")
[ $c -eq 0 ] && echo $word
done
If you're using bash, it will be faster if you use built-in tools rather than subshells.
line=grub2-systemd-sleep-plugin-2.02-153.1-foo-1
while [[ -n "$line" ]]
do if [[ "$line" =~ ^[0-9.]+- ]]
then line="${line#*-}"
elif [[ "$line" =~ ^[0-9.]+$ ]]
then break
elif [[ "$line" =~ ^([[:alnum:]]+)- ]]
then echo "${BASH_REMATCH[1]}";
line="${line#*-}"
elif [[ "$line" =~ ^([[:alnum:]]+)$ ]]
then echo "${BASH_REMATCH[1]}";
break
else echo "How did I get here?"
fi
done
or if you prefer,
shopt -s extglob
line=grub2-systemd-sleep-plugin-2.02-153.1-foo-1
while [[ -n "$line" ]]
do case "$line" in
+([0-9.])-*) line="${line#*-}" ;;
+([0-9.])) break ;;
+([[:alnum:]])-*) echo "${line%%-*}"
line="${line#*-}" ;;
+([[:alnum:]])) echo "$line"
break ;;
*) echo "How did I get here?" ;;
esac
done

Trying to validate PS3 input based on menu output for user validation

If i wanted to validate a null return from a user keyboard input if the user hasn't selected an option to reprint the menu how could this be achieved? and if a invalid choice is made to clear the screen / reprint the menu but insure the error message is still visible? maybe capture a return code as part of the exit and based on that provide an error?
The file ALIAS_FILE.config consists of these values:
ALIAS1 CLUSTER1
ALIAS2 CLUSTER2
ALIAS3 CLUSTER3
QUIT
Script:
mapfile -t arr < "/var/ALIAS_FILE.config"
select alias_select in "${arr[#]}"
do
if [[ -z "$alias_select" ]]
then
echo
echo "Invalid Choice"
echo
elif [[ $alias_select == "QUIT" ]]
then
break
else
echo
echo "You selected: $alias_select"
echo
ALIAS=$(echo "${alias_select}" | awk '{print $1}')
CLUSTER=$(echo "${alias_select}" | awk '{print $2}')
echo "$ALIAS"
echo "$CLUSTER"
fi
done
As long as you have your file (/var/ALIAS_FILE.config), you should be able to show the menu anytime, you also have access to the values in arr through mapfile here is a quick attempt, with some advices:
use echo -e or event better (and recommended) printf
clear screen, you can use VT100 escape code printf "\033c"
use bash string manipulation
updated script:
#!/bin/bash
mapfile -t arr< "/var/ALIAS_FILE.config"
print_menu(){
for (( i=0; i<${#arr[#]}; i++ ));
do
echo $i")" ${arr[$i]}
done
}
printf "\033c"
select alias_select in "${arr[#]}"
do
printf "\033c"
if [[ -z "$alias_select" ]]
then
echo -e "\nInvalid Choice\n"
print_menu
elif [[ $alias_select == "QUIT" ]]
then
break
else
echo -e "\nYou selected: $alias_select\n"
echo -e "${alias_select// *}\n${alias_select##* }"
fi
done

checking if a string is a palindrome

I am trying to check if a string is a palindrome in bash. Here is what I came up with:
#!/bin/bash
read -p "Enter a string: " string
if [[ $string|rev == $string ]]; then
echo "Palindrome"
fi
Now, echo $string|rev gives reversed string. My logic was to use it in the condition for if. That did not work out so well.
So, how can I store the "returned value" from rev into a variable? or use it directly in a condition?
Another variation without echo and unnecessary quoting within [[ ... ]]:
#!/bin/bash
read -p "Enter a string: " string
if [[ $(rev <<< "$string") == "$string" ]]; then
echo Palindrome
fi
A bash-only implementation:
is_palindrome () {
local word=$1
local len=$((${#word} - 1))
local i
for ((i=0; i <= (len/2); i++)); do
[[ ${word:i:1} == ${word:len-i:1} ]] || return 1
done
return 0
}
for word in hello kayak; do
if is_palindrome $word; then
echo $word is a palindrome
else
echo $word is NOT a palindrome
fi
done
Inspired by gniourf_gniourf:
is_palindrome() {
(( ${#1} <= 1 )) && return 0
[[ ${1:0:1} != ${1: -1} ]] && return 1
is_palindrome ${1:1: 1}
}
I bet the performance of this truly recursive call really sucks.
Use $(command substitution):
#!/bin/bash
read -p "Enter a string: " string
if [[ "$(echo "$string" | rev)" == "$string" ]]; then
echo "Palindrome"
fi
Maybe it is not the best implementation, but if you need something with pure sh
#!/bin/sh
#get character <str> <num_of_char>. Please, remember that indexing is from 1
get_character() {
echo "$1" | cut -c "$2"
}
for i in $(seq $((${#1} / 2))); do
if [ "$(get_character "$1" "$i")" != "$(get_character "$1" $((${#1} - i + 1)))" ]; then
echo "NO"
exit 0
fi
done
echo "YES"
and canonical way with bash as well
for i in $(seq 0 $((${#1} / 2 - 1))); do
if [ "${1:$i:1}" != "${1:$((${#1} - i - 1)):1}" ]; then
echo "NO"
exit 0
fi
done
echo "YES"
Skipping all punctuation marks and letter case.
input:He lived as a devil, eh?
output:Palindrome
input:Madam, I am Adam.
output:Not Palindrome
#!/bin/bash
#set -x
read -p "Enter a sentence" message
message=$(echo "$message" | \
sed -e '
s/[[:space:]]//g
s/[[:punct:]]//g
s/\!//g
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
' )
i=0
while read -n 1 letter
do
tempArray[i]="$letter"
((i++))
done < <(echo "$message")
i=0
counter=$((${#message}-1))
while [ "$i" -ne $((${#message}/2)) ]
do
if [ "${tempArray[$i]}" = "${tempArray[$counter]}" ]
then
((i++))
((counter--))
else echo -n "Not ";break
fi
done
echo "Palindrome"
exit

greping a character from file UNIX.linux bash. Can't pass an argument(file name) through command line

I am having trouble with my newbie linux script which needs to count brackets and tell if they are matched.
#!/bin/bash
file="$1"
x="()(((a)(()))"
left=$(grep -o "(" <<<"$x" | wc -l)
rght=$(grep -o ")" <<<"$x" | wc -l)
echo "left = $left right = $rght"
if [ $left -gt $rght ]
then echo "not enough brackets"
elif [ $left -eq $rght ]
then echo "all brackets are fine"
else echo "too many"
fi
the problem here is i can't pass an argument through command line so that grep would work and count the brackets from the file. In the $x place I tried writing $file but it does not work
I am executing the script by writting: ./script.h test1.txt the file test1.txt is on the same folder as script.h
Any help in explaining how the parameter passing works would be great. Or maybe other way to do this script?
The construct <<< is used to transmit "the contents of a variable", It is not applicable to "contents of files". If you execute this snippet, you could see what I mean:
#!/bin/bash
file="()(((a)((a simple test)))"
echo "$( cat <<<"$file" )"
which is also equivalent to just echo "$file". That is, what is being sent to the console are the contents of the variable "file".
To get the "contents of a file" which name is inside a var called "file", then do:
#!/bin/bash
file="test1.txt"
echo "$( cat <"$file" )"
which is exactly equivalent to echo "$( <"$file" )", cat <"$file" or even <"$file" cat
You can use: grep -o "(" <"$file" or <"$file" grep -o "("
But grep could accept a file as a parameter, so this: grep -o "(" "$file" also works.
However, I believe that tr would be a better command, as this: <"$file" tr -cd "(".
It transforms the whole file into a sequence of "(" only, which will need a lot less to be transmitted (passed) to the wc command. Your script would become, then:
#!/bin/bash -
file="$1"
[[ $file ]] || exit 1 # make sure the var "file" is not empty.
[[ -r $file ]] || exit 2 # test if the file "file" exists.
left=$(<"$file" tr -cd "("| wc -c)
rght=$(<"$file" tr -cd ")"| wc -c)
echo "left = $left right = $rght"
# as $left and $rght are strictly numeric values, this integer tests work:
(( $left > $rght )) && echo "not enough right brackets"
(( $left == $rght )) && echo "all brackets are fine"
(( $left < $rght )) && echo "too many right brackets"
# added as per an additional request of the OP.
if [[ $(<"$file" tr -cd "()"|head -c1) = ")" ]]; then
echo "the first character is (incorrectly) a right bracket"
fi

Linux bash script -

I am trying to use whether or not a line contains a date as a condition for an if statement:
if [grep -n -v '[0-9][0-9][0-9][0-9]' $line |wc -l==0]
then
...
The above returns an error. I don't necessarily need to use grep. The line processed by grep would look like:
1984 Dan Marino QB Miami Dolphins
Any help is appreciated.
if [[ $(echo $line | grep -q '[0-9][0-9][0-9][0-9]') ]]; then
# do something
fi
You can check this using bash built-ins:
re='\b[[:digit:]]{4}\b'
if [[ $line =~ $re ]] ; then
echo ok;
fi
[grep -n -v '[0-9][0-9][0-9][0-9]' $line |wc -l==0]
problem 1: [(space).....(space)] you need those spaces
problem 2: there is no [ foo==bar ] you can do something like [ $(echo "0") = "0" ] or [[ $(echo "0") == 0 ]] here the $(echo "0") is an example, you should fill with your commands.
You can just call grep with -q option and check the return value:
if [ $(grep -qv '[0-9][0-9][0-9][0-9]' $line) -eq 0 ]; then
# ...
fi
Use command substitution and proper bash syntax.
[[ "`grep -n -v '[0-9][0-9][0-9][0-9]' $line | wc -l`" -eq 0 ]]

Resources