Bash 通过用户获取输入,以执行“最后一次”和“切割”命令
我想提示用户从“last”命令中剪切,同时剪切哪些列。我还想使用“tr”来修复空白中的所有差异。这是我的代码:Bash 通过用户获取输入,以执行“最后一次”和“切割”命令,bash,shell,scripting,cut,tr,Bash,Shell,Scripting,Cut,Tr,我想提示用户从“last”命令中剪切,同时剪切哪些列。我还想使用“tr”来修复空白中的所有差异。这是我的代码: echo -n "What rows and columns would you like to cut: " read num_int read num_row read num_list last $num_int cut -f $num_row tr -d $num_list 当我从命令行运行它时,我会得到提示,所以我按enter键,然后输入一个要剪切的数字。然后我再次点击回车
echo -n "What rows and columns would you like to cut: "
read num_int
read num_row
read num_list
last $num_int
cut -f $num_row
tr -d $num_list
当我从命令行运行它时,我会得到提示,所以我按enter键,然后输入一个要剪切的数字。然后我再次点击回车键,我得到一个信息列表,它打印到屏幕上,但不会脱离执行的脚本。有没有更好的方法来设置此脚本?下面是一些在屏幕上显示的示例数据:
slater pts/2 78.189.121.247 Sat Sep 1 23:21 - 23:27 (00:05)
slater pts/3 77.189.121.247 Sat Sep 1 23:09 - 23:21 (00:12)
slater pts/2 76.189.121.247 Sat Sep 1 22:59 - 23:09 (00:10)
slater pts/2 74.189.121.247 Sat Sep 1 22:51 - 22:56 (00:05)
pint pts/2 74.189.121.247 Sat Sep 1 22:49 - 22:51 (00:01)
terry pts/2 74-119-247-134.r Sat Sep 1 19:08 - 19:09 (00:00)
我真的不明白你在这里想干什么。但我很确定您希望通过管道将这些命令相互传递,以便它们对彼此的输出进行操作:
last | cut -f $num_row | tr -d $num_list
last$num\u int
可能不符合您的要求last
需要用户名作为参数。要获取最后一行输出,请尝试tail
(或者head
):
我对
tr
命令特别困惑。你想用它做什么?这样做符合你的需要吗
read -p "What row and column integers would you like to cut >>> " col row
last | awk -v col=$col -v row=$row 'NR==row{print $col}'
您希望输出是什么样子的?你能举个例子吗?我只想去掉带有“tr”的非打印字符。这就是我要找的。我本来应该用awk的。非常感谢。
read -p "What row and column integers would you like to cut >>> " col row
last | awk -v col=$col -v row=$row 'NR==row{print $col}'