Questions tagged «bash»

与其他Bourne / POSIX shell相对,GNU的Bourne Again SHell特有的问题。对于一般关于Unix shell的问题,请改用/ shell标记。

2
完成的后台工作状态中的“退出2”是什么?
我有一个练习,将一些数据(某些目录的* conf)放入文件中,并且需要在后台进行。我做到了,我想知道输出消息的含义是什么: [A@localhost tests]$ ls -ld /etc/*conf /usr/*conf > test1_6_conf.txt 2>&1 & 输入升起此行: [1] 2533 这是什么意思?在其他回车之后,出现另一条消息 [A@localhost tests]$ [1]+ Exit 2 ls --color=auto -ld /etc/*conf /usr/*conf > test1_6_conf.txt 2>&1 这是什么意思?什么是“出口2”? 输入检查结果-似乎一切正常。 [A@localhost tests]$ [A@localhost tests]$ ls -l test1_6_conf.txt -rw-rw-r--. 1 A A 2641 Nov 22 14:19 test1_6_conf.txt [A@localhost tests]$ 我正在使用CentOS …



2
通过ssh运行命令时未获取点文件
当我交互式运行程序时,它可以正常运行: ssh somehost $ ~/some/path/somescript.py 当我直接通过ssh运行程序时,它不起作用。该变量PYTHONPATH未设置,因为.bashrc它不是源变量。 ssh somehost ~/some/path/somescript.py 如果我运行ssh somehost 'source ~/.bashrc; ~/some/path/somescript.py',它将正常运行。 但是后者不适用于其他一些人,例如,使用tcsh却根本不使用a ~/.bashrc)。 通过ssh在适用于所有shell的另一台主机上运行事物的命令是什么?

1
Bash尝试编写两个shell提示?
我正在查看连接到终端的正在运行的bash进程的strace输出,以进行教学。 我的bash进程具有PID 2883。 我输入 [OP@localhost ~]$ strace -e trace=openat,read,write,fork,vfork,clone,execve -p 2883 2> bash.strace 进入终端。然后,我进入bash流程,并进行以下交互: [OP@localhost ~]$ ls 看输出,我看到 strace: Process 2883 attached read(0, "l", 1) = 1 write(2, "l", 1) = 1 read(0, "s", 1) = 1 write(2, "s", 1) = 1 read(0, "\r", 1) = 1 write(2, "\n", 1) …
11 bash  tty  strace 

1
为什么不能为while循环反转输入重定向操作符的顺序?
在Bash中,您可以将输入重定向操作符移动到命令的前面: cat <<< "hello" # equivalent to <<< "hello" cat 为什么while循环无法执行相同的操作? while read -r line; do echo "$line"; done <<< "hello" # hello <<< "hello" while read -r line; do echo "$line"; done # -bash: syntax error near unexpected token `do' 我发现这有点令人困惑,因为您可以使用while循环。我是在做错什么还是仅仅是设计决定?

3
为什么sh(不是bash)抱怨.bashrc中定义的函数?
打开终端会话时,我得到了这个: sh:导入“ read.json”的函数定义时出错 sh:导入“ ts-project”的函数定义时出错 sh不喜欢这些功能,因为它们看起来像: read.json(){ :: } 和 ts-project(){ :: } 真正的问题是-为什么要sh触摸/解释这些文件?我在MacOS上,以前曾经看过,这真是个谜。我认为只有bash会加载这些文件。 更新:bash和sh并没有什么不同。当我在终端中输入bash时,得到以下信息: alex$ bash beginning to load .bashrc finished loading .bashrc bash-3.2$ 当我sh在终端输入时,得到以下信息: alex$ sh sh: error importing function definition for `read.json' sh: error importing function definition for `ts-project' sh-3.2$

2
为什么使用这种bash管道结构会丢失数据?
我正在尝试合并一些这样的程序(请忽略任何额外的包含,这是繁重的工作): pv -q -l -L 1 < input.csv | ./repeat <(nc "host" 1234) 重复程序的来源如下所示: #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/epoll.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <iostream> #include <string> inline std::string readline(int fd, const size_t len, const char delim = '\n') { std::string …

2
Bash脚本仅应杀死已启动的另一个脚本的实例
在当前情况下,某个脚本“ calling.sh”会在后台启动另一个脚本“ call.sh”,执行其他操作,休眠一会儿,然后以a终止“ call.sh” pkill called.sh。这很好。 然后,我还想在其他任何时候从其他终端以独立脚本的形式启动“ call.sh” ,无论是在启动calling.sh之前还是之后。这些独立的实例不应被“ calling.sh”杀死。 我该如何实现?直觉说,调用脚本应该能够告诉进程它是从与此同时运行的任何其他同名开始的。 作为变体,“ calling.sh”也可以启动“被调用”,这是“被调用.sh” 的符号链接。这会使上述情况的管理复杂化吗?使用符号链接需要进行哪些特定的警告和调整?
11 bash  process 

2
为什么不能将变量用作设置环境变量的命令的前缀?
通常,可以通过为命令设置前缀来设置环境变量,如下所示: hello=hi bash -c 'echo $hello' 我也知道我们可以使用变量来替代命令调用的任何部分,如下所示: $ cmd=bash $ $cmd -c "echo hi" # equivalent to bash -c "echo hi" 我很惊讶地发现您不能使用变量来为设置环境变量的命令添加前缀。测试用例: $ prefix=hello=hi $ echo $prefix # prints hello=hi $ $prefix bash -c 'echo $hello' hello=hi: command not found 为什么不能使用变量设置环境变量?前缀部分是特殊的部分吗?我可以通过在前面使用eval使其工作,但是我仍然不明白为什么。我正在使用bash 4.4。

3
陷阱'Ctrl + c'用于bash脚本,但不用于在该脚本中打开进程
我试图在bash脚本中有一个交互式程序: my_program 我希望能够使用“ Ctrl + c”将其关闭。但是,当我这样做时,我的脚本也会关闭。 我知道 trap '' 2 my_program trap 2 但是在这种情况下,我无法my_program使用Ctrl + c 关闭。 您是否知道如何在程序上允许Ctrl + c而不关闭运行脚本的脚本? 编辑:添加示例 #!/bin/bash my_program my_program2 如果我使用Ctrl + c关闭my_program,my_program2则不会执行,因为会退出整个脚本。
11 bash  trap 

1
Bash使用参数列表有性能问题吗?
在bash 5.0中解决 背景 对于背景(和理解(并试图避免这个问题似乎吸引人们的投票)),我将解释导致该问题的途径(嗯,两个月后我能记得的最好的)。 假设您正在对一些Unicode字符进行shell测试: printf "$(printf '\\U%x ' {33..200})" 并且有超过一百万个Unicode字符,测试其中的20.000个字符似乎并不多。 还假设您将字符设置为位置参数: set -- $(printf "$(printf '\\U%x ' {33..20000})") 目的是将字符传递给每个函数以不同的方式处理它们。因此,函数应具有形式test1 "$@"或类似形式。现在,我意识到这在bash中是多么糟糕的主意。 现在,假设需要时间(n = 1000)每个解决方案以找出哪个更好,在这种情况下,您将得到一个类似于以下的结构: #!/bin/bash -- TIMEFORMAT='real: %R' # '%R %U %S' set -- $(printf "$(printf '\\U%x ' {33..20000})") n=1000 test1(){ echo "$1"; } >/dev/null test2(){ echo "$#"; } >/dev/null …
11 linux  bash  time 

2
为什么GNU Bash手册中没有“时间”?
[fakename]$ help time time: time [-p] pipeline Report time consumed by pipeline's execution... 由此看来,似乎time是Bash内置的。但是,我无法在此页面上找到其描述:https : //www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html#Shell-Builtin-Commands。为什么会这样呢?
11 bash  man  time 

2
文件描述符的寿命是多少?
如上所述这里,使用重定向open()写入到一个文件中。在外壳程序中创建了一个内部(?)文件描述符,然后在需要时使用它。 内部描述符是在脚本或Shell生命周期的整个过程中创建的吗?经过一段时间,多次操作等,它会被销毁吗? 我的意思是特别是shell本身为其内置操作打开的文件的文件描述符。是否为每个操作创建了描述符并打开了文件?他们保留了多久?例: #!/bin/bash >>x echo something ...do many other things not related to the file x >>x echo something more 是否将第一个描述符实例保留到第二个操作? 我在终端中使用的外壳呢?有时,我每天开放一次会议,甚至可能持续数周。它是否仍保留我使用Shell内置程序操作的所有文件的描述符?

8
基于if条件的案例失败
我正在寻找一种方法来根据bash中的case条件中的if条件发生失败。例如: input="foo" VAR="1" case $input in foo) if [ $VAR = "1" ]; then # perform fallthrough else # do not perform fallthrough fi ;; *) echo "fallthrough worked!" ;; esac 在上面的代码中,如果变量VAR是1,我希望条件条件执行失败。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.