Linux - Check if string is in list [duplicate] - linux

This question already has answers here:
Check if a variable exists in a list in Bash
(21 answers)
Closed 5 years ago.
I have a bash script and I want to check if a string is in a list.
Like: string = "Hello World!", List=("foo", "bar").
Python example:
if name in list: # Another way -> if name in ["foo", "bar"]
# work to do
else:
sys.exit(1)
Thank you!

There are a number of ways, the simplest I see is:
#!/bin/sh
WORD_LIST="one two three"
MATCH="twox"
if echo "$WORD_LIST" | grep -qw "$MATCH"; then
echo "found"
else
echo "not found"
exit 1
fi

Related

linux shell scripting error: ./mp.sh: line 10: [: missing `]' File ] is unavailable [duplicate]

This question already has answers here:
How can I compare two floating point numbers in Bash?
(22 answers)
'&&' vs. '&' with the 'test' command in Bash
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 months ago.
I am trying to build a simple BMI calculator for my college project but I am having error while using if statements. I am new to shell scripting so I don't know much about it. I think the problem is because I am using bc command because if remove the if statements it still gives me 0 answer.
Here is my code
# --------------------BMI calculator---------------------
weight="0"
height="0"
BMI="0"
echo "Welcome to BMI calculator"
echo "Please enter your weight in kilograms: "
read weight
echo "Please enter your height in meters: "
read height
if [ "$height > 0" | bc ] & [ "$weight > 0" | bc ]
then
BMI="$weight/($height*$height)" | bc
echo "Your BMI is $BMI"
else
echo "Invalid inputs!!"
fi
Please let me know if there are any other error as well

In Powershell, how to turn a string into a cmd? [duplicate]

This question already has answers here:
How do you execute an arbitrary native command from a string?
(4 answers)
Closed 6 months ago.
Let's say I want to get a specific dir contents:
$cmd = "dir"
$args = ".yarn*"
$output = "$cmd $args"
echo $output
My output contains 2 string literals:
dir .yarn*
Expected (as a command on 1 line, opposed to a "string" on separate lines):
dir *yarn
#iRon 's comment with a suggested answer was correct:
Invoke-Expression($output) is the answer!

Bash script if not processing as I would expect [duplicate]

This question already has answers here:
Why equal to operator does not work if it is not surrounded by space?
(4 answers)
How to compare strings in Bash
(12 answers)
Multiplication on command line terminal
(8 answers)
Closed 3 years ago.
I am in the very early stages of learning Unix scripting. I've written a Bash script which does not generate any errors, but clearly has a logic error, as the IF test always gives the same response.
I have tried various variations on the theme of my IF, but still end up with the same result.
#!/bin/bash
declare -i number1
declare -i number2
declare -i total
declare operation
echo "Enter a, s, m or d for add, subtract, multiply or divide"
read operation
echo "Enter number 1"
read number1
echo "Enter number 2"
read number2
echo "operation="$operation
if [ $operation=='m' ]
then
total=$number1*$number2
elif [ $operation=='a' ]
then
total=$number1+$number2
elif [ $operation=='d' ]
then
total=$number1/$number2
elif [ $operation=='s' ]
then
total=$number1-$number2
fi
echo $number1 " multiplied by " $number2 " equals " $total
exit 0
It doesn't matter whether I enter a, s or d (or indeed m) in response to the first prompt, my script always does a really nice multiplication... The line
echo "operation="$operation
correctly shows the operator I've requested.
Any ideas what I've done wrong?
Many thanks
You need to add spaces around all the ==. That's it. Instead of
if [ $operation=='m' ]
you should have:
if [ $operation == 'm' ]

Bash declaring variable with a number inside a for loop [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 7 years ago.
I can't find an answer to this problem, other than people asking to use an array instead. That's not what I want. I want to declare a number of variables inside a for loop with the same name except for an index number.
I=0
For int in $ints;
Do i=[$i +1]; INTF$i=$int; done
It doesn't really work. When I run the script it thinks the middle part INTF$i=$int is a command.
Without an array, you need to use the declare command:
i=0
for int in $ints; do
i=$((i +1))
declare "intf$i=$int"
done
With an array:
intf=()
for int in $ints; do
intf+=( $int )
done
Bash doesn't handle dynamic variable names nicely, but you can use an array to keep variables and results.
[/tmp]$ cat thi.sh
#!/bin/bash
ints=(data0 data1 data2)
i=0
INTF=()
for int in $ints
do
((i++))
INTF[$i]=$int
echo "set $INTF$i INTF[$i] to $int"
done
[/tmp]$ bash thi.sh
set 1 INTF[1] to data0

Command not found [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
I am new to shell scripting
Below is my script
#!/bin/bash
first_num = 0
second_num = 0
echo -n "Enter the first number =>"
read first_num
echo -n "Enter the second number =>"
read second_num
echo "first + second = $((first_num + second_num))"
Whenever I run it, it prints like below
/Users/haani/arithmetic.bash: line 3: first_num: command not found
/Users/haani/arithmetic.bash: line 4: second_num: command not found
Enter the first number =>
What could be the reason for command not found here?
try without spaces:
first_num=1

Resources