Bash conditional pipe to select statement [duplicate] - bash

This question already has an answer here:
Conditionally add a component to a shell pipeline
(1 answer)
Closed 5 days ago.
I want to have a conditional pipe to a select loop, I have found several answers on conditional pipes but haven't been able to get any to work. Is something like this possible?
if [ $condition -eq 0 ]; then
select action in "${actions[#]}"; do
elif [ $condition -eq 1 ]; then
sleep 0.1 | yes 1 | select action in "${actions[#]}"; do
fi
[[ ! "$REPLY" =~ $isNum ]] && continue
# Edit because someone is getting upset
#"${action[#]}"
done
This give the error syntax error near unexpected token elif

Block statements need to be nested properly, you can't use a conditional around just the statement that starts a block.
If you want to avoid writing the select block twice, put it in a function.
do_select() {
select action in "${actions[#]}"; do
[[ ! "$REPLY" =~ $isNum ]] && continue
"${action[#]}"
done
}
if [ $condition -eq 0 ]; then
do_select
else
sleep 0.1 | yes 1 | do_select
fi

Related

bash if string starts with character [duplicate]

This question already has answers here:
Checking whether a string starts with XXXX
(5 answers)
Closed 6 months ago.
This is very basic but eluding me. I have an array. I am checking each element of this array to see if it starts with given character. I think it what i have written is right but not getting desired response.
My code
arr=("etpass-foo" "etpass-bar" "pass-foo" "pass-abc" "etpass-abc")
for i in "${arr[#]}"
do
if [[ $i == et* ]]; then
printf "$i"
fi
done
I get below output
etpass-foo
etpass-bar
pass-foo
pass-abc
etpass-abc
What is expect is
etpass-foo
etpass-bar
etpass-abc
I have also tried below if conditions
1. if [[ $i == et* ]]; then
2. if [[ "$i" == "et"* ]]; then
3. if [[ "$i" == "et*" ]]; then
Please let me know where i am wrong?
Try to use bashregex instead, like this:
...
if [[ $i =~ ^et.* ]]; then
echo "$i"
fi
...

Access variables using an external string.[SHELL script]

I currently have code as
if [[ "$FIRSTFLAG" == 1 ]] ; then
all_comp+=("FIRST")
fi
if [[ "$SECONDFLAG" == 1 ]] ; then
all_comp+=("SECOND")
fi
if [[ "$THIRDFLAG" == 1 ]] ; then
all_comp+=("THIRD")
fi
all_comp is just an array
So, im working on a solution to reduce the repetitive code
I know that we can use case here.
I wonder if there is a solution that can be done using array and for loop \
For example(I know its syntactically wrong)
names=("FIRST" "SECOND" "THIRD")
for i in $names[#]; do
if [[ ${i}FLAG == 1 ]]; then <- This line is the issue
all_comp+=("$i")
fi
done
So please tell me if there is a solution for such code example
You need to use indirect expansion by saving the constructed variable name, e.g. iflag=${i}FLAG, then you can use access the indirect expansion with ${!iflag}, e.g.
FIRSTFLAG=1
SECONDFLAG=0
THIRDFLAG=1
all_comp=()
names=("FIRST" "SECOND" "THIRD")
for i in ${names[#]}; do
iflag=${i}FLAG
if [[ ${!iflag} == 1 ]]; then
all_comp+=("$i")
fi
done
echo ${all_comp[#]} # Outputs: FIRST THIRD
Oh another answer, you can make use of the arithmetic expansion operator (( )) i.e.
FIRSTFLAG=1
SECONDFLAG=0
THIRDFLAG=1
all_comp=()
names=("FIRST" "SECOND" "THIRD")
for i in ${names[#]}; do
if (( ${i}FLAG == 1 )); then
all_comp+=("$i")
(( ${i}FLAG = 99 ))
fi
done
echo ${all_comp[#]} # FIRST THIRD
echo $FIRSTFLAG # 99
echo $SECONDFLAG # 0
echo $THIRDFLAG # 99
Reference:
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion

If condition in shell script fails [duplicate]

This question already has answers here:
How to compare strings in Bash
(12 answers)
Closed last year.
I would like to what is the issue in this script , why this if condition is getting passed
i see that HI is printed , but type = A this if condition should not pass
[omega#ctsedgenode 1.1.11-SNAPSHOT_V12]$ type="A"
[omega#ctsedgenode 1.1.11-SNAPSHOT_V12]$ if [[ $type -eq "C" || $type -eq "D" ]]; then echo
"HI" ; fi
HI
[omega#ctsedgenode 1.1.11-SNAPSHOT_V12]$
Try with this instead:
if [[ $type == "C" || $type == "D" ]]; then echo "HI" ; fi

Writing a do while loop in bash with multiple conditions

I'm having some trouble writing a do-while loop in bash with multiple conditions.
My code currently works when it is like this:
while
count=$((count+1))
( MyFunction $arg1 $arg2 -eq 1 )
do
:
done
But I want to add a second condition to the "do-while" loop like so:
while
count=$((count+1))
( MyFunction $arg1 $arg2 -eq 1 ) || ( $count -lt 20 )
do
:
done
When I do this I get an "command not found error".
I've been trying some of the while loop examples from this post but had no luck and the do-while example I use is from here. In particular the answer with 137 likes.
The ( is part of syntax and $count is not a valid command. The test or [ is a valid command that is used to "test" expressions.
while
count=$((count+1))
[ "$(MyFunction "$arg1" "$arg2")" -eq 1 ] || [ "$count" -lt 20 ]
do
:
done
The answer you mention uses arithmetic expressions with (( (not a single (, but double (( without anything between). You could also do:
while
count=$((count+1))
(( "$(MyFunction "$arg1" "$arg2")" == 1 || count < 20 ))
do
:
done
You can use for loop:
for ((count=0; i<20 && $(MyFunction $arg1 $arg2) == 1; count++)); do
echo $count
done

How to transform if elif to getopts

i've been working on transforming if elif statement to getopts for sometime but i can't seems to get it. I would like to run getopts statement without inserting any options.
if [ $? eq 0 ]; then
echo "username exist"
exit 1
elif grep $uid
echo"$uid"
elif grep $gid
echo"$gid"
might be some stupid question, but yeah. just started learning with bash. Thanks for your patients.

Resources