我想写一个非常简单的脚本,它接受一个进程名,并返回包含进程名的最后一个文件名的尾部。
我写了类似的东西:
#!/bin/sh
tail $(ls -t *"$1"*| head -1) -f
我的问题:
我需要第一行吗?
为什么不是
ls -t *"$1"*|头-1 | tail -f
工作?有更好的方法吗?
最佳答案
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/