echo output not getting assigned to a variable in shell script

喜夏-厌秋 提交于 2020-03-25 19:24:30

问题


end=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 2 |rev
begin=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 3 |rev
echo $end
echo $begin

echo abc_11204.00_15713.00_.csv  | rev | cut -d'_' -f 2 |rev ---- This works

But echo $end is not printing anything

I even tried:

 set end=echo abc_11204.00_15713.00_.csv  | rev | cut -d'_' -f 2 |rev
 echo $end 

This prints empty

Please help me with this

Sample input : abc_123.00_345.00_xyz.csv
Output : end=345.00
         begin=123.00

回答1:


Could you please try following. Easy approach with awk.

start=$(echo "$input_variable" | awk  -F'_' '{print $2}')
end=$(echo "$input_variable" | awk  -F'_' '{print $3}')

When I print variable's values it will be as follows:

echo "$start"
123.00
echo "$end"
345.00


来源:https://stackoverflow.com/questions/60147694/echo-output-not-getting-assigned-to-a-variable-in-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!