bash eval in if with piped command [duplicate] - bash

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Compare output rather than command
(3 answers)
Closed 3 years ago.
piped eval in bash for string length comparison
i am trying to check if a certain device with a given id is plugged in and trigger a action based on that
i tried eval / exec
here is what i have so far
#!/bin/bash
KBP='[["lsusb -d 1c11:b04d | wc -c" == "0"]]'
if eval $KBP; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
expected result:
if device is plugged in and string is not 0 it would hop in the false condition
actual result - cant evaluate the piped condition

Guessing fixed expression would look like this:
if [ "$(lsusb -d 1c11:b04d | wc -c)" -eq 0 ]; then
To remember:
Bash is spaces aware. [[ and ]] needs after and behind (well, ; is special here, it separates commands).
To get output of a command use command substitution $( ... )
There is no need for eval here.

Related

what does it mean inside the function job_id=$1 flag=$2 in Shell script [duplicate]

This question already has answers here:
What do $? $0 $1 $2 mean in shell script? [duplicate]
(2 answers)
What does it mean when shell command starts with a dot?
(1 answer)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Meaning of $? (dollar question mark) in shell scripts
(8 answers)
Closed 2 months ago.
In shell script in the middle there is one function as
#!/bin/ksh
var=$?
.......
FILE_NAME="/dir/user/work/file/abc.txt"
. ${FILE_NAME}
. ${HOME_PATH}/script/abc.sh
.....
func_start() {
JOB_ID=$1
FLAG=$2
NODES=$3
while [$i -le $NODES]
........ }
exit 0
What does it mean here in this script as var=$?, . ${file name} & function func_start() job_id=$1 flag=$2 nodes=$3? I am not much clear about this.
$? returns the exit value of the last executed command. echo $? prints that value on console. zero implies a successful execution while non-zero values are mapped to various reason for failure.
$1 $2 $3 : according to GNU
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using the set builtin command.

what is the -n operator in bash shell script and stand for? [duplicate]

This question already has answers here:
What does -n mean in Bash?
(2 answers)
Closed 3 years ago.
I found this -n operator in bash shell script. I don't have any clue about this and searched, but didn't find any helpful resource.
This is the script code:
#!/bin/bash
while [ -n "$1" ] ....
I also tried to get the output of the -n like:
#!/bin/bash
echo -n #return nothing
Your help will be appreciated!
-n string returns true if the length of the string is non-zero.
This is documented in Bash Conditional Expressions

bash sh script always executes regardless of parameter [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 4 years ago.
I have the following script
#!/bin/bash
if [ $1=="1" ]
then
echo $1
fi
Whenever I run ./myscript.sh 0 it still prints "0". I am not sure why? It prints whatever I type in because the if executes. What would I need to change?
Add proper spaces, i.e. before and after == inside if condition
#!/bin/bash
if [ $1 == "1" ]
then
echo $1
fi

Hi everyone! can someone tell me what " if [ $# -ne 1 ]"means in a shell script? [duplicate]

This question already has answers here:
Special variables in Unix shells? [closed]
(4 answers)
Is there a list of 'if' switches anywhere?
(5 answers)
Closed 5 years ago.
I have been trying to figure out the following line of code as well:
if [ ! -e $1 ]
thanks
Lets break it down:
$# is the number of remaining arguments
[ is the test command
-ne is the numeric "not equals" operator.
So if [ $# -ne 1 ] is testing if there is exactly one argument (left).
In your second example:
! means not
-e tests if a file exists
$1 is the first remaining argument
Therefore if [ ! -e $1 ] tests that there is no file or directory whose path is given as the first (remaining) argument.
Note that this may fail if the argument is a pathname containing whitespace or globing meta-characters. Quoting is needed to stop word splitting and globbing potentially mangling the pathname; i.e. if [ ! -e "$1" ]

Bash- if clause doesn't work correctly [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
I want to make a shutdown script and it doesn't work as intended. This is what I wrote.
echo "Wanna shutdown (y/n)?"
read ANSWER
if [ $ANSWER=="y" ]
then
sudo shutdown -P now
else
printf "Something...."
whatever i press it just shuts down. Why?
You need to put spaces around the == operator. Otherwise the test expression is a single word, and all non-empty words test successfully.
In addition, if you wish to be portable, you should use = instead of ==. And it is always wise to double quote variable expansions because [ won't do that for you.
if [ "$ANSWER" = y ]; then
On the other hand, if you are using bash (or ksh or zsh) you could use the more forgiving [[ conditional expression:
if [[ $ANSWER = y ]]; then

Resources