我们可以看到rm命令的概要是:
rm [OPTION]... [FILE]...
Run Code Online (Sandbox Code Playgroud)
这不是意味着我们只能使用rm命令而没有任何选项或参数吗?
当我自己运行命令rm时,终端会显示以下错误:
Run Code Online (Sandbox Code Playgroud)rm: missing operand Try 'rm --help' for more information.
谁能告诉我为什么会这样?
我有如下一行:
find /foo/bar -name '*.mp4' -print0 | xargs -i {} -0 mv -t /some/path {}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
xargs: argument line too long
Run Code Online (Sandbox Code Playgroud)
我很迷惑。不xargs应该使用' should'来精确地帮助解决这个问题吗?
注意:我知道我可以在技术上使用-execfind,但我想了解为什么上述失败,因为我的理解xargs是应该知道如何将输入拆分为可管理的大小以使其运行的参数。这不是真的吗?
这都是zsh。
在其他地方,我看到了一个 cd 函数,如下所示:
cd()
{
builtin cd "$@"
}
Run Code Online (Sandbox Code Playgroud)
为什么建议使用$@而不是$1?
我创建了一个测试目录“r st”并调用了包含这个函数的脚本,它以任何一种方式工作
$ . cdtest.sh "r st"
Run Code Online (Sandbox Code Playgroud)
但 $ . cdtest.sh r st无论我使用"$@"还是失败"$1"
我试图将多个参数传递给一个函数,但其中一个由两个词组成,我希望 shell 函数将它作为一个参数来处理:
args=("$@")
function(){
echo ${args[0]}
echo ${args[1]}
echo ${args[2]}
}
Run Code Online (Sandbox Code Playgroud)
当我调用这个命令时 sh shell hi hello guys bye
我明白了
hi
hello
guys
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是:
hi
hello guys
bye
Run Code Online (Sandbox Code Playgroud) 我很困惑在为以下程序编写 bash 脚本时如何包含可选参数/标志:
该程序需要两个参数:
run_program --flag1 <value> --flag2 <value>
Run Code Online (Sandbox Code Playgroud)
但是,有几个可选标志:
run_program --flag1 <value> --flag2 <value> --optflag1 <value> --optflag2 <value> --optflag3 <value> --optflag4 <value> --optflag5 <value>
Run Code Online (Sandbox Code Playgroud)
我想运行 bash 脚本,以便它接受用户参数。如果用户只按顺序输入两个参数,那么它将是:
#!/bin/sh
run_program --flag1 $1 --flag2 $2
Run Code Online (Sandbox Code Playgroud)
但是如果包含任何可选参数呢?我认为它会是
if [ --optflag1 "$3" ]; then
run_program --flag1 $1 --flag2 $2 --optflag1 $3
fi
Run Code Online (Sandbox Code Playgroud)
但是如果给了 4 美元而不是 3 美元呢?
我可以使用任一形式来执行该cat方法:
cat file_name
cat < file_name
Run Code Online (Sandbox Code Playgroud)
结果是一样的
然后我想以man格式执行stdin
man < file_name
Run Code Online (Sandbox Code Playgroud)
虽然file_name包含:
# file_name
cat
Run Code Online (Sandbox Code Playgroud)
但它弹出
What manual page do you want?而不是 execute man cat。
我想知道为什么cat可以接受stdin作为参数但man不能。命令行参数和stdin?之间有什么区别?
证人:
$ ps f
PID TTY STAT TIME COMMAND
31509 pts/3 Ss 0:01 -bash
27266 pts/3 S+ 0:00 \_ mysql -uroot -p
25210 pts/10 Ss+ 0:00 /bin/bash
24444 pts/4 Ss 0:00 -bash
29111 pts/4 S+ 0:00 \_ tmux attach
4833 pts/5 Ss+ 0:00 -bash
9046 pts/6 Ss 0:00 -bash
17749 pts/6 R+ 0:00 \_ ps f
4748 pts/0 Ss 0:00 -bash
14635 pts/0 T 0:02 \_ mysql -uroot -px xxxxxxxxxxxxxxxx
16210 pts/0 S+ 0:01 \_ mysql -uroot -px xxxxxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)
ps …
我想知道什么时候应该使用管道,什么时候不应该使用。
例如,要杀死某些处理 pdf 文件的进程,使用管道将无法执行以下操作:
ps aux | grep pdf | awk '{print $2}'|kill
Run Code Online (Sandbox Code Playgroud)
相反,我们只能通过以下方式做到这一点:
kill $(ps aux| grep pdf| awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)
或者
ps aux | grep pdf | awk '{print $2}'| xargs kill
Run Code Online (Sandbox Code Playgroud)
根据man bash(版本4.1.2):
The standard output of command is connected via a pipe to the standard input of command2.
Run Code Online (Sandbox Code Playgroud)
对于上述场景:
grep是标准输出ps。那个有效。awk是标准输出grep。那个有效。kill是标准输出awk。那行不通。以下命令的标准输入总是从前一个命令的标准输出获得输入。
killor 一起使用rm?我想编写一个参数数量未知的 bash 脚本。
我怎样才能解决这些争论并用它们做点什么?
错误的尝试如下所示:
#!/bin/bash
for i in $args; do
echo $i
done
Run Code Online (Sandbox Code Playgroud) 如果我有以下 2 个文件和 1 个文件夹:
someuser@computer:~/Desktop/test$ ls -l
total 340
-rw-r--r-- 1 someuser someuser 45082 ago 5 09:56 file1.pdf
-rw-r--r-- 1 someuser someuser 291836 ago 5 09:56 file2.pdf
drwxrwxr-x 2 someuser someuser 4096 ago 5 09:56 this_is_a_folder.pdf
Run Code Online (Sandbox Code Playgroud)
然后我运行以下命令(注意我省略了目的地):
cp *.pdf
Run Code Online (Sandbox Code Playgroud)
file1.pdf并file2.pdf复制到this_is_a_folder.pdf文件夹中。
someuser@computer00:~/Desktop/test$ ls this_is_a_folder.pdf/
file1.pdf file2.pdf
Run Code Online (Sandbox Code Playgroud)
显然*.pdf是扩展到匹配项,所以它等价于
cp file1.pdf file2.pdf this_is_a_folder.pdf
Run Code Online (Sandbox Code Playgroud)
...和this_is_a_folder.pdf文件夹一样,然后将两个文件复制到其中。
这是一个错误吗?
这显然是通配符扩展的副作用,这不是我所期望的。
我会预料到一个missing destination file 错误。