Questions tagged «bash»

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

2
sudo:无法执行./script.sh:没有这样的文件或目录
我很沮丧 我的/home目录中有一个可执行的脚本: [user@server ~]$ ll total 4 -rwx------ 1 user user 2608 Jul 15 18:23 qa.sh 但是,当我尝试使用sudo它运行时说找不到它: [user@server ~]$ sudo ./qa.sh [sudo] password for user: sudo: unable to execute ./qa.sh: No such file or directory 这是一个全新的版本。没有进行会导致问题的更改。实际上,脚本的目的是确保它实际上是根据我们的政策构建的。也许不是,sudo实际上在构建过程中被破坏了吗? 我还应该注意,我可以sudo在其他目录中使用其他命令来运行。 编辑:脚本(我没有写过,所以请不要/bin/bash在上面写;)) #! /bin/bash . /root/.bash_profile customer=$1 if [ -z "$customer" ]; then echo …

6
使用Bash脚本重命名多个文件
我想使用Bash脚本重命名同一目录中的多个文件。文件名如下: file2602201409853.p file0901201437404.p file0901201438761.p file1003201410069.p file2602201410180.p 我想重命名为以下格式: file2503201409853.p file2503201437404.p file2503201438761.p file2503201410069.p file2503201410180.p 我正在阅读有关重命名命令的内容,并尝试通过这种方式进行操作,但是它没有任何作用,我认为我对语法有疑问。然后我读到您可以使用mv命令进行循环,如下所示: for file in cmpsms*2014*.p; do mv "$file" "${file/cmpsms*2014*.p/cmpsms25032014*.p}" done 但是我无法重命名文件。我究竟做错了什么?
27 bash  rename 

4
为什么没有任何Shell命令来创建文件?
请注意: 我不是在问如何从命令行制作文件! 多年来,我一直touch在制作文件而没有注意它的主要目的是其他东西。如果要从命令行创建文件,则有很多可能性: touch foo.bar > foo.bar cat > foo.bar echo -n > foo.bar printf '' > foo.bar 而且我敢肯定还有更多。 但是事实是,以上命令实际上都不是为创建文件而设计的。例如,man touch建议此命令用于更改文件时间戳。为什么没有像Unix(或Linux)那样完整的OS具有专门用于创建文件的命令?


5
使bash使用外部`time`命令而不是内置的shell
如何让bash默认使用时间二进制(/ usr / bin / time)而不是shell关键字? which time收益/usr/bin/time type time回报time is a shell keyword 运行time显然是执行外壳关键字: $ time real 0m0.000s user 0m0.000s sys 0m0.000s $ /usr/bin/time Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose] [--portability] [--format=format] [--output=file] [--version] [--quiet] [--help] command [arg...] enable -n time 退货 bash: enable: time: not …

5
如何列出当前shell的bash选项?
bash解释器本身具有选项,即 bash手册页的第22-23行: OPTIONS All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked: -c ... -i ... -l ... -r ... 我在bash手册页中使用了一些搜索模式,例如: /^\s*set /list …
27 bash  shopt 

5
如何使用while循环从两个输入文件中读取
我想知道是否有任何一种方式可以同时嵌套嵌套循环读取两个输入文件。例如,假设我有两个文件FileA和FileB。 FileA: [jaypal:~/Temp] cat filea this is File A line1 this is File A line2 this is File A line3 文件B: [jaypal:~/Temp] cat fileb this is File B line1 this is File B line2 this is File B line3 当前示例脚本: [jaypal:~/Temp] cat read.sh #!/bin/bash while read lineA do echo $lineA …

3
Bash自动完成:首先列出文件,然后循环浏览它们
是否可以通过以下方式配置bash,使其在第一个选项卡自动完成时列出所有可能的文件,并在随后的循环中进行选择? 这两个选项很容易分别完成,我可以将它们绑定到不同的键,但是上面的方法很完美,但是我在网上找不到任何关于它的信息。

3
我该如何计时管道?
我想要time一个命令,该命令由两个单独的命令组成,一个管道输出到另一个管道。例如,考虑以下两个脚本: $ cat foo.sh #!/bin/sh sleep 4 $ cat bar.sh #!/bin/sh sleep 2 现在,我如何才能time报告所花费的时间foo.sh | bar.sh(是的,我知道这里的管道没有意义,但这只是一个示例)?如果我在不使用管道的子外壳中依次运行它们,则可以正常工作: $ time ( foo.sh; bar.sh ) real 0m6.020s user 0m0.010s sys 0m0.003s 但是在管道传输时我无法使其工作: $ time ( foo.sh | bar.sh ) real 0m4.009s user 0m0.007s sys 0m0.003s $ time ( { foo.sh | bar.sh; } …
27 bash  shell  pipe  time 

2
包装脚本中执行的原因
我看过以下概括的包装器脚本示例: #!/bin/bash myprog=sleep echo "This is the wrapper script, it will exec "$myprog"" exec "$myprog" "$@" 如上所示,它们用于exec几乎立即用替换新创建的外壳$myprog。如果没有exec以下内容,则可以实现相同的目的: #!/bin/bash myprog=sleep echo "This is the wrapper script, it will exec "$myprog"" "$myprog" "$@" 在最后一个示例中,启动了一个新的bash实例,然后$myprog将其作为bash实例的子进程启动。 第一种方法的好处是什么?
27 bash  exec 


5
具有数字顺序的球
我在目录中有以下pdf文件列表: c0.pdf c12.pdf c15.pdf c18.pdf c20.pdf c4.pdf c7.pdf c10.pdf c13.pdf c16.pdf c19.pdf c2.pdf c5.pdf c8.pdf c11.pdf c14.pdf c17.pdf c1.pdf c3.pdf c6.pdf c9.pdf 我想使用ghostscript以数字顺序将它们连接起来(类似于此): gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf *.pdf 但是,shell扩展顺序不重现数字的自然顺序,而是按字母顺序: $ for f in *.pdf; do echo $f; done c0.pdf c10.pdf c11.pdf c12.pdf c13.pdf c14.pdf c15.pdf c16.pdf c17.pdf …

1
如果“ bash <文件>”有效,为什么“源<文件>”抛出错误?
我有以下脚本: #!/bin/bash set -x if :; then echo a fi 如果我运行bash /tmp/file,a则会被回显,但是如果我运行source /tmp/file,则会得到: bash: /tmp/test: line 6: syntax error: unexpected end of file 输出: knezi@holly tmp]$set -x; source /tmp/test; set +x + source /tmp/test ++ set -x bash: /tmp/test: line 6: syntax error: unexpected end of file + set +x …

2
TERM环境变量默认值在哪里设置?
当我在桌面GUI中使用GNOME终端仿真器打开终端窗口时,shell TERM环境变量默认为值xterm。 如果我使用CTL+ ALT+ F1切换到控制台TTY窗口,并且echo $TERM该值设置为linux。 我发问的动机是,在我的~/.bashrc文件中使用一个变量来确定是否提供了彩色外壳或仅是老式的单色。 # set a fancy prompt (non-color, unless we know we "want" color) case "$TERM" in xterm-color) color_prompt=yes;; esac 在控制台外壳和Gnome Terminal模拟器外壳中,如果我键入 export TERM=xterm-color source /.bashrc 两个壳都更改为颜色模式(我想总是在这两者中发生某些事情)。 请在哪里TERM设置默认值?如果可能,最好在哪里更改默认值?终端仿真器GUI中似乎没有任何东西可以选择或设置默认的TERM值。 我确实考虑过仅将行添加export TERM=xterm-color到~/.bashrc文件的顶部,但是我的直觉告诉我这不是最好的解决方案,而且我的Google搜索还没有给我一个很好的答案。 我正在运行Ubuntu 15.04桌面版(基于Debian)。

1
Ctrl C与Ctrl Z与前景工作
Ctrl+ Z停止工作,而Ctrl+ C取消工作。 这是为什么?另一种方式会更有意义吗? z@z-lap:~$ sleep 100&amp; [1] 4458 z@z-lap:~$ sleep 200&amp; [2] 4459 z@z-lap:~$ jobs [1]- Running sleep 100 &amp; [2]+ Running sleep 200 &amp; z@z-lap:~$ fg %1 sleep 100 ^Z [1]+ Stopped sleep 100 z@z-lap:~$ jobs [1]+ Stopped sleep 100 [2]- Running sleep 200 &amp; z@z-lap:~$ fg %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.