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
Related
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:
bash script do not save output file although required
(3 answers)
Closed 1 year ago.
I am trying to cut out the prefix from a variable that represents path.
Currently this is my code:
for f in /Users/username/Documents/Dev/beneficiary-service/src/main/helm/*
do
echo $f
if [[ $f == 'values'* ]]
then
yq d -i $f 'resources.'
fi
done
I printed $f to see its output. I expected it to be ONLY the filename, without the path (values-stg.yaml).
However, this is the output:
+ echo /Users/username/Documents/Dev/beneficiary-service/src/main/helm/values-stg.yaml
/Users/username/Documents/Dev/beneficiary-service/src/main/helm/values-stg.yaml
+ [[ /Users/username/Documents/Dev/beneficiary-service/src/main/helm/values-stg.yaml == \v\a\l\u\e\s* ]]
And also, the "if" statement will never be true, because it considers values* literally as is and not as "anything that starts with values"
Path expansion includes the whole path specified. You can remove it using parameter expansion
[[ ${f##*/} == values* ]]
the "if" statement will never be true
That's not true. Quoting values is not needed, though, as none of the characters is special.
Use Basename - demonstration below - assigned output of basename "${f}" (in case of white space in file names)
for f in /Users/username/Documents/Dev/beneficiary-service/src/main/helm/*
do
echo $f
bf=$(basename "${f}") ; echo ${bf}
if [[ ${bf} == 'values'* ]]
then
echo yq d -i ${bf} 'resources.'
fi
done
This question already has answers here:
Remove carriage return in Unix
(21 answers)
Closed 6 years ago.
I write a scipt like this:
#!/bin/bash
while read line
do
echo line ${line}
pdbfile=${line}.pdb
echo pdbfile ${pdbfile}
done < myfile
The result is:
line pdb8mhtA
.pdbfile pdb8mhtA
While it is supposed to be
line pdb8mhtA
pdbfile pdb8mhtA.pdb
What's wrong with this? Why the string concatenation does not work? And why the strange dot at the beginning of the line?
I replace with pdbfile=${line}'.pdb'. That does not change the result.
The "string goes to the beginning of the line" is symptomatic of a carriage return in your $line that you can among many other ways remove with a tr pipe to your file:
while IFS= read -r line
do
echo "line ${line}"
pdbfile=${line}.pdb
echo "pdbfile ${pdbfile}"
done < <(tr -d '\r' <file)
I've tried your script and it works fine for me:
./testConcat
line pdb8mhtA
pdbfile pdb8mhtA.pdb
btw you could try to "preserve" the "."
while read line
do
echo line ${line}
pdbfile=${line}\.pdb
echo pdbfile ${pdbfile}
done < myfile
as you can see the result is the same
./testConcat
line pdb8mhtA
pdbfile pdb8mhtA.pdb
This question already has answers here:
How to trim whitespace from a Bash variable?
(52 answers)
Closed 7 years ago.
For example, I have a string, " some string", and I want to put "some string" in another string variable. How do I do that?
My code:
function get_title() {
t1=$(get_type "$1")
t2="ACM Transactions"
t3="ELSEVIER"
t4="IEEE Transactions"
t5="MIT Press"
if [ "$t1"=="$t2" ];
then
title=$(less "$1" | head -1)
elif [ "$t1"=="$t5" ];
then
title=$(less "$1" | head -3)
fi
echo "$title"
}
As you can see the $title can return unwanted whitespace in front of text in center aligned texts. I want to prevent that.
A robust and straightforward approach is to use sed e.g.
$ sed 's/^[[:space:]]*//' <<< "$var"
If you are willing to turn on extended globbing (shopt -s extglob), then the following will remove initial whitespace from $var:
"${var##+([[:space:]])}"
Example:
var=$' \t abc \t ' echo "=${var##+([[:space:]])}="
=abc =
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 6 years ago.
For example:
#!/bin/sh
a=0
while [ "$a" -lt 10 ]
b="$a"
while [ "$b" -ge 0 ] do
echo -n "$b "
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done*
The above mentioned script gives the answer in triangle while with out the double quotes, it falls one after the other on diff lines.
After a variable is expanded to its value, word splitting (i.e. separating the value into tokens at whitespace) and filename wildcard expansion takes place unless the variable is inside double quotes.
Example:
var='foo bar'
echo No quotes: $var
echo With quotes: "$var"
will output:
No quotes: foo bar
With quotes: foo bar
Here the difference is how the argument is passed to echo function. Effectively " " will preserve whitespaces.
This:
echo -n "$b "
Is translated to:
echo -n "<number><space>"
While this:
echo -n $b<space>
Will ignore the trailing space and will just output the number:
echo -n <number>
Therefore removing all the spaces that are needed for output to look "triangular".
There are errors in your script:
no do after 1st while
no ; before do after 2nd while
why asterisk on done* at the end?
Now to answer your question.
If used as a paramenter:
"$a" is one argument.
$a (without quotes) is possibly multiple arguments:
Compare:
v='a b'; set $v; echo "\$#=$#, \$1=\"$1\", \$2=\"$2\""
$#=2, $1="a", $2="b"
v='a b'; set "$v"; echo "\$#=$#, \$1=\"$1\", \$2=\"$2\""
$#=1, $1="a b", $2=""