This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 4 years ago.
I seem to be having some problems executing this script? not sure what is causing the problem?
touch file
cat file | while read line;
do
output=$(awk '/path/ {print NR, $0}' "$line");
if [$(#output) -ne 0];
then
echo "File: " "$line";
echo "----------------------------------------------------";
echo "$output"
else
echo "No keyword appeared";
fi
done
Based on shellcheck.net
Line 5:
if [$(#output) -ne 0];
^-- SC1035: You need a space after the [ and before the ].
^-- SC1009: The mentioned syntax error was in this test expression.
^-- SC1073: Couldn't parse this command expansion. Fix to allow more checks.
Line 6:
then
^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.
Related
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 4 months ago.
I use this bash code to test whether two strings are equal, but the result is not as what I exptect. I have used "" to wrap two strings and used [[]] rather than [] in if condition, but still failed. Where did I do wrong? Thanks in advance.
#!/bin/bash
touch a.txt b.txt
date -r a.txt +%y-%m-%d > b.txt
A="$(cat b.txt)"
B="$(date -r a.txt +%y-%m-%d)"
if [["$A" == "$B"]]
then
echo "equal"
else
echo "not equal"
fi
Error is listed below.
➜ ~ chmod a+x test.sh
➜ ~ ./test.sh
./test.sh: line 6: [[22-10-13: command not found
not equal
You need to put a space between [[ and "$A" and between "$B" and ]].
This is because in bash, [, ], [[ and ]] are commands:
$ type [
[ is a builtin
Weird right?
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.
This question already has answers here:
Difference between single and double square brackets in Bash
(7 answers)
How can I escape a double quote inside double quotes?
(9 answers)
Closed 6 months ago.
I have a problem with bash, I'm trying to detect strings in a file with bash with the file looking like
cnlog "Hello World!"
When i try the [ $variable = *" ] in bash it doesn't work and throws out an error
basilc.sh: line 13: unexpected EOF while looking for matching `"'
basilc.sh: line 16: syntax error: unexpected end of file
the code of the bash file is
char=""
while IFS='' read -n1 c; do
if [ $char = '"' ] || [ $char = *" ]
then
echo "STRING FOUND"
char=""
pwd
fi
echo "$char"
done < $1
Please help
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
This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 5 years ago.
lines=`grep -c "" List`
a=1
while [$a -lt $lines]
do
b=`sed "${a} q;d" List`
a=$a+1
echo $b
done
So, I have this, what is supposed to take a line from a list and echo it.
Now, the while loop is not working(?) and returns me this error:HARR.sh: line 9: syntax error: unexpected end of file
What is wrong with it?
Is this what you want?
#!/bin/bash
#
let a=1
while read line; do
echo "$a $line"
let a+=1
done < List