简体   繁体   English

Shell脚本,将命令值保存到变量

[英]Shell script, saving the command value to a variable

I am trying to print the value of VARI in the same line followed by a comma, so that i can have a csv file of these values, but im not able to save the value of VARI = 'cat filename | head -1 | cut -d, -f${i}' 我正在尝试在同一行中打印VARI的值,后跟一个逗号,以便我可以拥有这些值的csv文件,但无法保存VARI = 'cat filename | head -1 | cut -d, -f${i}'的值VARI = 'cat filename | head -1 | cut -d, -f${i}' VARI = 'cat filename | head -1 | cut -d, -f${i}'

i=0
while (( i<130)) ;
do
  if [[ $i -eq 1 ||  $i -eq 9 || $i -eq 12 || $i -eq 23 || $i -eq 25 || $i -eq 29 ]]
  then
    VARI = 'cat filename | head -1 | cut -d, -f${i}'
    echo  "$VARI ,"   
  fi
  let i=$i+1;
done

output expected is 预期输出为

4,abc,5,8,xyz,9

Please let me know what i am doing wrong, thanks! 请让我知道我做错了,谢谢!

use backticks (or $() – can be nested), not single quotes: 使用反引号(或$() –可以嵌套),而不是单引号:

VARI=`cat filename | head -1 | cut -d, -f${i}` # or:
VARI=$(cat filename | head -1 | cut -d, -f${i})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM