Questions tagged «shell»

Shell是Unix的命令行界面。您可以交互地在外壳程序中键入命令,也可以编写脚本来自动执行任务。使用此标记可解决适用于/ bin / sh和大多数兼容shell(ash,bash,ksh,zsh等)的问题。对于有错误的Shell脚本,请在此处发布之前在http://shellcheck.net中对其进行检查。



4
$ @除了第一个参数
我需要编写一个以这种方式运行的shell脚本: ./myscript arg1 arg2_1 arg2_2 arg2_3 ....... arg2_# 脚本中有一个for循环 for i in $@ 但是,据我所知,$ @包含$ 1到$($#-1)。但是对于我的程序,$ 1与$ 2 $ 3 $ 4等有明显的不同。我想从$ 2循环到结尾...我如何实现这一点?谢谢:)
36 bash  shell  scripting 

5
如何在执行之前读取整个shell脚本?
通常,如果您编辑脚本,则脚本的所有运行用法都容易出错。 据我了解,bash(也有其他shell吗?)以增量方式读取脚本,因此,如果从外部修改脚本文件,它将开始读取错误的内容。有什么办法可以防止呢? 例: sleep 20 echo test 如果执行此脚本,bash将读取第一行(例如10个字节)并进入睡眠状态。恢复时,脚本中可能有不同的内容(从第10个字节开始)。我可能在新脚本的一行中间。因此,正在运行的脚本将被破坏。
35 shell 

8
git clone后如何自动CD?
我想在输入某些内容后自动cd进入clone命令创建的目录git clone。 重要提示:我不想更改命令的语法(例如,使用别名/函数),因为它会破坏我从Pretzo项目中自动获得的zsh完成。 编辑:之所以没有选择正确的答案,是因为没有给出符合上述条件的答案。 我使用ZSH,但在其他外壳程序中的答案也是可以接受的。
35 shell  zsh 

3
了解反引号(`)
我正在尝试命令 $ b=5; echo `$b`; -bash: 5: command not found 但它不会按预期打印5。我在这里想念什么? 命令中的`(反引号/反引号)是什么意思?似乎说可以`评估其中的命令并将其替换为输出。


5
为什么我的〜/ .bash_profile不起作用?
我正在使用Linux Mint。我的登录shell(cat /etc/passwd | grep myUserName)是bash。 在启动图形桌面环境并从中运行终端仿真器之后,我可以看到它.bash_profile不是源代码(其中设置的环境变量未export设置)。但是,如果我从一个文本控制台登录(ctrl+ alt+ F1)或手动运行bash -l从终端仿真器,.bash_profile工作正常。 我认为.bash_profile应该在X启动时export获取该资源并且从X运行的所有ed var应在终端中可用,我错了吗? PS将所有内容.bashrc放进去并从中购买.bash_profile不是一个好主意(https://stackoverflow.com/questions/902946/):环境材料应仅获得一次。
35 bash  shell  login  profile 


2
如何从csh更改为bash作为默认shell
我将其csh作为默认外壳,如所示echo $SHELL。我想切换到bash默认外壳程序。我尝试了以下方法无济于事: 随着chsh我得到: chsh: can only change local entries; use ypchsh instead. 随着ypchsh我得到: ypchsh: yppasswdd not running on NIS master host ("dcsun2"). 我只有.chsrc主目录,.profile在中找不到任何文件/etc。如何将默认外壳程序更改为bash?
34 shell 

3
GNU查找并掩盖了某些shell的{}-哪个?
GNU查找手册页指出: -exec command ; [...] The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with …
34 shell  find  xargs  quoting 


3
$ {param:= value}是什么意思?
我在《 Z-Shell用户指南》中阅读了以下内容: 'true'的同义词是':';它通常以这种形式用于提供具有副作用但不应该使用的参数-类似于 : ${param:=value} 这是所有Bourne Shell衍生物中的常见习语。在参数扩展中,$param如果之前为空,则为其赋值,否则保留该值。由于那是参数扩展的唯一原因,因此您:可以忽略该参数。实际上,shell巧妙地构建了命令行-冒号,然后跟随$paramis 的值(无论赋值是否发生),然后执行命令;碰巧':'并没有注意到它给出的参数。 但我不明白。我的:意思是true,但是表达式中有两个冒号。作为一个小问题,为什么这个习语在所有的Bourne Shell衍生物中都使用了这么多?它有什么作用? 注意: 我对该成语在bash和zsh中的作用感兴趣。 谢谢
34 bash  shell  zsh 

7
以另一个(非root)用户身份运行GUI应用程序
假设我有2个用户帐户user1和user2。当我以身份登录user1,然后切换为user2使用时su,我可以执行命令行程序,但是GUI程序失败。 例: user1@laptop:~$ su - user2 user2@laptop:~$ leafpad ~/somefile.txt No protocol specified leafpad: Cannot open display: 那么如何运行GUI应用程序?
34 shell  x11  xorg  su 

5
我们如何运行存储在变量中的命令?
$ ls -l /tmp/test/my\ dir/ total 0 我想知道为什么以下运行上述命令的方法失败还是成功? $ abc='ls -l "/tmp/test/my dir"' $ $abc ls: cannot access '"/tmp/test/my': No such file or directory ls: cannot access 'dir"': No such file or directory $ "$abc" bash: ls -l "/tmp/test/my dir": No such file or directory $ bash -c $abc 'my …
34 bash  shell  quoting  variable 

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.