我有简单的脚本:
#!/bin/sh
#NOTE - this script does not work!
#column=${1:-1}
column=1+1
awk '{print $'$column'}'
但是运行的时候
ls -la | ~/test/Column.sh
我总是收到
1
1
1
1
什么问题?
我有简单的脚本:
#!/bin/sh
#NOTE - this script does not work!
#column=${1:-1}
column=1+1
awk '{print $'$column'}'
但是运行的时候
ls -la | ~/test/Column.sh
我总是收到
1
1
1
1
什么问题?
您的脚本相当于:
awk '{print $1+1}'
awk 尝试ls -al
在示例中的输出的第一列中添加一个,即文件类型和模式位。这不是一个数字,所以它会被转换为 0。将 1 加到 0,就可以得到输出。
见这里:
无法解释为有效数字的字符串将转换为零。
如果您希望外壳计算数字,请尝试:
column=$(expr 1 + 1)
甚至
column=$((1 + 1))
如果这是纯 bourne shell,而不是:
column=1+1
试试这个:
column=`expr 1+1`
如果是 bash,它应该是:
column=$[1+1]
见这里: http: //linuxreviews.org/beginner/bash_GNU_Bourne-Again_SHell_Reference/#toc12