search for a variable in a string in bash [duplicate] - bash

This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 6 months ago.
I have a string given below:
string1 = "Hello there, my name is Jack.
Hello there, my name is Jack.
Hello there, my name is Jack."
I'm taking the following input from the string:
read string2
I want to check whether the string2(which is a variable) is present in string1.
I tried running the below command:
output=$(echo $string1 | grep -o "$string2")
echo $output
eg: Let string2="name"
The output is empty when I'm running this command.
Can someone tell me where am I going wrong?

#!/bin/bash
string1="Hello there, my name is Jack"
string2="name"
if [[ $string1 == *"$string2"* ]]; then
echo "$string2 found"
else
echo "$string2 not found"
fi

Alternate method with POSIX-shell grammar:
string1='Hello there, my name is Jack'
string2='name'
case "$string1" in
*"$string2"*) printf '%s found\n' "$string2";;
*) printf '%s not found\n' "$string2";;
esac

Related

bash setting variable getting error command not found [duplicate]

This question already has answers here:
Indirect variable assignment in bash
(7 answers)
Closed 9 months ago.
I'm writing one small bash script for my Project
but i getting some errors
#!/bin/bash
count=$(cat Downloads/datei.csv | wc -l);
for((i=115;i<="122";i++)); do
line$i=$(sed -n $i"p" Downloads/datei.csv);
echo "line$i";
done
i trying to get every line from CSV in some variable
The erroĊ•
count.sh: line 4: line115=: command not found
if [[ -z "line$i" ]];
then
echo "$i is empty";
else
echo "$i is NOT empty";
fi
the second code gives me the same error
Suggest compose the code together, just update line$i to a temporary normal variable without other var like line_get.
One more thing, you need to update -z "line$i" to -z "$line_get". As in bash, you should use $ and the var name to get the value.
Looks like:
#!/bin/bash
count=$(cat Downloads/datei.csv | wc -l)
for((i=115;i<=122;i++)); do
line_get=$(sed -n $i"p" Downloads/datei.csv)
if [[ -z "$line_get" ]];
then
echo "$i is empty"
else
echo "$i is NOT empty"
fi
done
If you need to dynamic generate the variable name. Try this:
declare "line$i=$(sed -n $i"p" Downloads/datei.csv)"
var="line$i"
echo "${!var}"
And echo "line$i"; just print the text like line115 but not the value of the variable line$1.

How to compare a variable to a string in bash? [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 2 years ago.
here is how i tried it
while IFS= read line
do
var=$(cut -d ":" -f 3 $line)
if [ "$var" = "L2" ]
then :here is my action:
fi
done < myfile.txt
What i want to do is read a file line by line, read the third word of each line, and do a special action if the third word = a certaine string, i've tried a lot of syntax but it doesn't work. i've also tried to echo "$var" just to see if my variable get the right value, and it does. i don't know what to do anymore
It is better to use double brackets for if condition & for String comparison double equals (==)
And the line which has "cut" command wouldn't have worked. Please find below the corrected code which is working.
while IFS= read line
do
echo "Line is $line"
var=`echo $line | cut -d ":" -f 3`
echo $var
if [[ "$var" == "L2" ]]
then
echo "Some Action"
fi
done < myfile.txt

Why does if condition give error in bash? [duplicate]

This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 6 years ago.
My code below:
echo "====================================="
echo " Test Programme "
echo "====================================="
echo
read -p "Enter Name: " name
if [$name -eq ""]; then
sleep 1
echo "Oh Great! You haven't entered name."
exit
fi
read -p "Enter age: " age
According to that code,I expected "Oh Great! You haven't entered name." to show up when user skips entering the name which WORKS WELL
But, when you enter a proper string for name, it gives this message/ error:
./cool_ham.sh: line 13: [Franco: command not found
I want to know the reason for that.
I have even tried "$name" = "" after #Jack suggested, but still din't work .
Put a space between the square braces of your if condition and its contents.
if [ "$name" = "" ]; then
Additionally note that I use = over -eq to compare strings. -eq is used to compare integer values, while = will compare strings, which can be unintuitive coming from other languages.
I also quoted $name to prevent globbing and word splitting.

How to write a multiline string to a file in Bash [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
I want to rewrite a configuration file when asked from a bash script. Here is my code.
function quality {
echo $1 > ~/.livestreamerrc
echo ".livestreamer was modified!"
}
best="stream-types=hls
hls-segment-threads=4
default-stream=best
player=vlc --cache 5000"
read -p "Set quality: " INPUT
if [[ "$INPUT" == "!best" ]]; then
quality $best
fi
This code does the following to .livestreamer file though.
$cat ~/.livestreamerrc
stream-types=hls
Why?
Change it to
quality "$best" # double quotes to avoid word splitting
and then
echo "$1" > ~/.livestreamerrc
Note : Worth checking the [ shellcheck ] documentation.Also, fully uppercase variables like INPUT are reserved for the system.

Bash - Get first 3 letters of filename [duplicate]

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 8 years ago.
I have a little bash script and have $file and $file2 variable. This is file and I want to get first 3 letters of this file name. And I want to compare them:
I tried:
curfile=$(basename $file)
curfilefirst3=${curfile:0:3}
curfile2=$(basename $file2)
curfile2first3=${curfile2:0:3}
if ((curfilefirst3 == curfile2first3 )); then
....
But I think have problems, how can I fix?
Thank you.
You're missing $ for the strings in the comparison and you'll need to wrap each string with " and the entire expression with []:
file="this.txt"
file2="that.txt"
curfile=$(basename $file)
curfilefirst3=${curfile:0:3}
curfile2=$(basename $file2)
curfile2first3=${curfile2:0:3}
echo $curfile2first3
echo $curfilefirst3
if [ "$curfile2first3" == "$curfilefirst3" ]
then
echo "same!"
else
echo "different!"
fi
It might be a good idea to read up on bash conditionals
Substring Extraction
${string:position} Extracts substring from $string at $position.
However, if should use [ and not ( as in:
if [ $curfilefirst3 == $curfile2first3 ]; then
The corrected version:
#!/bin/bash
file=abcdef
file2=abc123456
curfile=$(basename $file)
curfilefirst3=${curfile:0:3}
curfile2=$(basename $file2)
curfile2first3=${curfile2:0:3}
echo $curfilefirst3
echo $curfile2first3
if [ $curfilefirst3 = $curfile2first3 ]; then
echo same
else
echo different
fi
It prints same
So, works

Resources