linux - Shell 脚本 - Linux

标签 linux shell scripting

我想写一个非常简单的脚本,它接受一个进程名,并返回包含进程名的最后一个文件名的尾部。

我写了类似的东西:

#!/bin/sh
tail $(ls -t *"$1"*| head -1) -f

我的问题:

  1. 我需要第一行吗?

  2. 为什么不是 ls -t *"$1"*|头-1 | tail -f 工作?

  3. 有更好的方法吗?

最佳答案

1:第一行是所谓的she-bang,阅读描述here :

In computing, a shebang (also called a hashbang, hashpling, pound bang, or crunchbang) refers to the characters "#!" when they are the first two characters in an interpreter directive as the first line of a text file. In a Unix-like operating system, the program loader takes the presence of these two characters as an indication that the file is a script, and tries to execute that script using the interpreter specified by the rest of the first line in the file

2:tail 不能从标准输入中获取文件名:它可以将标准输入中的文本或文件作为参数。查看man page为此。

3:我没有想到更好的解决方案:注意包含空格的文件名:这不适用于您当前的解决方案,您需要在 $() block 周围添加引号。

关于linux - Shell 脚本 - Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2380584/

相关文章:

regex - 如何修改此正则表达式以从 ls -a 输出中获取文件名?

python + 运行带变量的系统命令

linux - 我们可以在 MPI 中获取进程运行的核心 ID 吗?

linux - 什么时候用引号括起 shell 变量?

linux - shell 脚本 -o 操作不起作用

linux - 如何在不重启 byobu 的情况下使 ~/.byobu/.tmux.conf 中的更改生效

linux - SSH 持久连接超时

bash - 如何解析 mmcli 输出中的调制解调器编号、标识符和名称?

ruby-on-rails - 在 ActiveRecord 模型类中重新加载列名称

Bash:从文件中读取包含单字或双字的变量