开发者

bash save values in variable

given: file: files.txt with:

sara.gap sara.gao
pe.gap pe.gao

I just want to use f=sara in my bash skript, b开发者_如何学运维ecause I need f later in the skript. so i tryed: get ffirst line,second argument,remove .gao and save in f

f=sed -ne '1p' files.txt |cut -d " " -f2 |sed 's/.gao//g' 

But did not work, please help me ;(


You just need backticks:

f=`head -1 files.txt | cut -d " " -f2 | sed 's/.gao//g'`


I'd do

read f junk < files.txt
f=${f%*.gap}

oh, and for second argument:

read junk f junk < files.txt
f=${f%*.gao}

That's completely in bash :-)


Use Command Substitution if you want to use the output of a command to set a variable. The format is v=$(command). You can also use backticks e.g. v=`command`, but this has been superseded by the $(...) form.

Your command would be:

f=$(sed -ne '1p' files.txt |cut -d " " -f2 |sed 's/.gao//g')
echo $f

prints

sara


you mean this?

f="sed -ne '1p' files.txt |cut -d ' ' -f2 |sed 's/.gao//g'"
eval "$f"

with output:

sara


You can use awk as well

f=$(awk 'NR==1{gsub(/\.gao/,"",$2);print $2;exit}' file)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜