Bash - Get first 3 letters of filename [duplicate] - bash

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

Related

`echo` is stripping newlines in Bash script [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
When should I double-quote a parameter expansion? [duplicate]
(1 answer)
Closed 5 months ago.
If I have a file containing newlines, the below script will output the file as is, with newlines:
#!/bin/bash
FOO=$(cat filename.yaml)
echo "$FOO"
but
#!/bin/bash
FOO=$(cat filename.yaml)
FOO=$(echo $FOO)
echo "$FOO"
outputs the file all on one line. How come?
I do not recommend storing the contents of entire files in a single variable. In my experience that can have unpredictable results.
/usr/bin/env bash -x
index=$(wc -l filename.yaml | cut -d' ' -f1)
count=1
next () {
[[ "${count}" -lt "${index}" ]] && main
[[ "${count}" -eq "${index}" ]] && exit 0
}
main () {
line=$(sed -n "${count}p" filename.yaml)
echo "var${count}=${line}" >> varfile
count=$(($count+1))
next
}
next
If you source varfile at the start of another script, it will give you every line from that file, in its' own variable.

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

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

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

bash - setting variable value is not working [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 5 years ago.
Problem
My bash script loops through a bunch of files and if it finds an unexpected file that contains some text, it sets an error flag and is supposed to exit the loop. The problem is that when i evaluate the error condition after the loop, the error flag isn't set properly.
I can't seem to see where the bug is.
Code
#!/bin/sh
valid=1
grep -rs -L 'bogustext' * | while read line; do
echo "Processing file '$line'"
if [ "$line" != "arc/.keep" ] && [ "$line" != "arc/prp/lxc" ] && [ "$line" != "arc/lbb/lxc" ]; then
valid=0
echo "just set it to zero - $valid"
break 2
fi
done
echo "$valid"
if [ "$valid" -eq 0 ]; then
echo "1..1"
echo "not ok $line invis malformed"
else
echo "1..1"
echo "ok"
fi
Here's the output which shows the problem / bug. As you can see, there is an extra file in the list that contains the bogus string "arc/ttp". The script sets the "valid" variable to 0 but by the time I'm ready to evaluate it to display the right status, it's still the original value.
lab-1:/var/vrior# sh test.sh
Processing file 'arc/.keep'
Processing file 'arc/prp/lxc'
Processing file 'arc/lbb/lxc'
Processing file 'arc/ttp'
just set it to zero - 0
1
1..1
ok
What I've tried so far
I'm currently googling to see if in Bash there's local variables vs. global. Not too sure, but if you see my bug, please let me know.
Pipes create subshells, so $valid inside the loop refers to a new variable, which disappears before you try and use it.
Since you've tagged bash, then you can use a process substitution instead of the pipe:
while read line; do
echo "Processing file '$line'"
if [ "$line" != "arc/.keep" ] && [ "$line" != "arc/prp/lxc" ] && [ "$line" != "arc/lbb/lxc" ]; then
valid=0
echo "just set it to zero - $valid"
break 2
fi
done < <(grep -rs -L 'bogustext' *)

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.

Resources