所以我写了我的第一个 bash 脚本:
#!/bin/bash
echo 'hello world!'
exit
Run Code Online (Sandbox Code Playgroud)
我知道它有正确的 bash 位置并且是可执行的:
$ which bash
/bin/bash
$ chmod +x myscript.sh
Run Code Online (Sandbox Code Playgroud)
现在我想从命令行运行它,但出现错误:
$ myscript.sh
myscript.sh: command not found
Run Code Online (Sandbox Code Playgroud)
所以我改为尝试这个并且它有效:
$ bash myscript.sh
hello world!
Run Code Online (Sandbox Code Playgroud)
这是我总是需要执行它的方式吗?我觉得我已经执行了其他脚本而不必在它之前加上bash. 如何运行 myscript.sh 而不必在它前面加上bash?
更新:这里很好地解释了为什么以及如何执行 bash 脚本。
如果我有一个/foo包含几个文件的目录,我如何将每个条目符号链接/foo到/bar/?
例如,如果/foo有文件a,b和c,我想创建三个符号链接:
/bar/a -> /foo/a/bar/b -> /foo/b/bar/c -> /foo/cUbuntu 10.10+
在我的脚本中,我需要查找给定主机名的 IP。
如果该名称在 中列出/etc/hosts,则命令应该打印来自 的 IP /etc/hosts,而不是来自 DNS 服务器。
我尝试过的命令 ( nslookup, dig, host) 完全忽略/etc/hosts- 至少对于 DNS 服务器不知道的名称。
注意:我更喜欢不需要我/etc/hosts手动grep的解决方案。
有没有办法“好像”在新的登录会话中运行命令?
我已经试过了env -i。但是,我不想处理必须设置或取消设置的各种 ENV 变量。
我也试过bash -c "some command"and bash -l -c "some commmand",但它们都是复制当前环境。
我最接近的是一个不太干净的解决方案:
ssh me@localhost "some command"`
Run Code Online (Sandbox Code Playgroud) 来自:http : //www.chriswrites.com/2012/02/how-to-find-and-delete-duplicate-files-in-mac-os-x/ 我如何修改这个只删除第一个版本它看到的文件。
从 Spotlight 或 Utilities 文件夹打开终端使用 cd 命令更改为要从中搜索(包括子文件夹)的目录(文件夹)。在命令提示符下键入 cd 例如 cd ~/Documents 将目录更改为您的主文档文件夹 在命令提示符下,键入以下命令:
find . -size 20 \! -type d -exec cksum {} \; | sort | tee /tmp/f.tmp | cut -f 1,2 -d ' ' | uniq -d | grep -hif – /tmp/f.tmp > duplicates.txt
Run Code Online (Sandbox Code Playgroud)
此方法使用简单的校验和来确定文件是否相同。重复项的名称将列在当前目录中名为duplicates.txt 的文件中。打开它以查看相同文件的名称 现在有多种方法可以删除重复项。要删除文本文件中的所有文件,请在命令提示符下键入:
while read file; do rm "$file"; done < duplicates.txt
Run Code Online (Sandbox Code Playgroud) 好的,所以我正在学习如何在我的 Mac 上安装 rvm 的教程。通过 curl 获取 rvm 的 bash 命令是
curl -L https://get.rvm.io | bash -s stable
Run Code Online (Sandbox Code Playgroud)
我了解 rvm.io 位置上前半部分的 curl 命令,并且结果通过管道传输到后续的 bash 命令,但我不确定该命令在做什么。我的问题:
-s :我总是对如何引用这些感到困惑。这是什么类型的东西:命令行参数?一个开关?还有什么?
-s :它在做什么?我已经用谷歌搜索了大约半个小时,但不知道如何引用它,这让它变得很困难。
稳定:这是什么?
tl;dr:帮我破译命令 bash -s stable
对于那些回答这篇文章的人,我渴望有一天能像你一样精通 bash。在此之前,像我这样的opstard 感谢您的帮助!
我想在按时间戳(创建时间)排序的子目录中查找文件,最近的在最前面。
我看过手册页,但它似乎只让你按时间戳过滤而不是按时间戳排序。
我可以通过哪些选项find来实现这一目标?或者,我如何将它与其他工具结合使用,比如sort按时间戳排序?
这是以下内容的手册页条目-n:
-n
抑制模式空间的自动打印
我注意到,当不使用-n对某些操作,每行被打印到标准输出(和所请求的线被印刷两次):
$ cat test.txt
first
second
third
fourth
fifth
$ sed -n '2,3p' test.txt
second
third
$ sed '2,3p' test.txt
first
second
second
third
third
fourth
fifth
Run Code Online (Sandbox Code Playgroud)
但是,该定律不适用于其他命令:
$ sed -n 's/t/T/' test.txt
$ sed 's/t/T/' test.txt
firsT
second
Third
fourTh
fifTh
Run Code Online (Sandbox Code Playgroud)
那么究竟有什么作用-n呢?
在文档中,我看到了两种用法:
find . -type f -exec file '{}' \;
find repo/ -exec test -d {}/.svn -o -d {}/.git -o -d {}/CVS ; \
Run Code Online (Sandbox Code Playgroud) 当我试图覆盖当前推出的二进制文件,cp无法覆盖,但它可能rm然后它cp。例如:
user@poste:~$ cp binaryFile /tmp
user@poste:~$ sudo cp /tmp/binaryFile binaryFile
[sudo] password for user:
cp: cannot create regular file `binaryFile`: Text file busy
user@poste:~$ sudo rm binaryFile
user@poste:~$ sudo cp /tmp/binaryFile binaryFile
user@poste:~$ file binaryFile
binaryFile : ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7ce005d9eb50e2574246b6a881e625802f7e49f2, not stripped
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?
bash ×10
linux ×6
command-line ×4
find ×2
unix ×2
dns ×1
hosts-file ×1
mac ×1
macos ×1
sed ×1
session ×1
shell ×1
syntax ×1
text-editing ×1
ubuntu ×1