我正在寻找一个简单的命令,可以在 Bash 中使用它来查找 OS X 上文件的绝对和规范化路径(类似于 Linux 下的“readlink -f”)。
以下示例 bash 会话描述了一个名为“abspath”的 [虚构] 实用程序,它展示了所需的行为:
$ pwd
/Users/guyfleegman
$ ls -lR
drwxr-xr-x 4 guyfleegman crew 136 Oct 30 02:09 foo
./foo:
-rw-r--r-- 1 guyfleegman crew 0 Oct 30 02:07 bar.txt
lrwxr-xr-x 1 guyfleegman crew 7 Oct 30 02:09 baz.txt -> bar.txt
$ abspath .
/Users/guyfleegman
$ abspath foo
/Users/guyfleegman/foo
$ abspath ./foo/bar.txt
/Users/guyfleegman/foo/bar.txt
$ abspath foo/baz.txt
/Users/guyfleegman/foo/baz.txt
Run Code Online (Sandbox Code Playgroud)
与上面示例中最后一次调用“abspath”一样,我希望它不会自动解析符号链接,但我不会在这里太挑剔。
有没有办法在脚本中切换用户身份(作为安装过程的一部分以 root 身份执行)以执行某些命令而不调用外部脚本,然后返回root运行其他命令?
有点:
#!/bin/bash
some commands as root
SWITCH_USER_TO user
some commands as user including environment variables checks, without calling an external script
SWITCH_USER_BACK
some other stuff as root, maybe another user id change...
Run Code Online (Sandbox Code Playgroud) Sudo root 权限是针对特定时间授予的,但仅限于一个终端。如果我想在那个时间段内在另一个终端中使用 sudo,我必须再次输入密码。如何强制 sudo 跨终端工作?
我需要将整个 linux 根文件系统从故障硬盘驱动器转移到另一台具有开放可用分区的计算机上。我很确定这涉及tarand ssh,但我不记得如何做到这一点。
我大概成像使用新/目标主机上的Live CD来运行的东西是这样的:
ssh user@failingharddrivehost "some tar command | piped into something else"
我在以下位置安装了旧版本的 Git:
/usr/bin/
Run Code Online (Sandbox Code Playgroud)
我最近下载了一个较新的版本:
/usr/local/bin
Run Code Online (Sandbox Code Playgroud)
当我输入:
which git
Run Code Online (Sandbox Code Playgroud)
我得到了旧版本的位置。我相信这只是因为 /usr/bin/ 出现在我的 $PATH 变量中的 /usr/local/bin 之前,所以旧版本的 git 首先被“找到”。
为了测试这一点,我将旧版本的 git 重命名为“git_old”。现在当我输入:
which git
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,我得到了较新版本的位置。但是当我输入:
git --version
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
-bash: /usr/bin/git: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我只是想知道为什么我的计算机要回到旧位置查找 Git?
我已经在我的电脑上安装了 Windows 10 周年更新。
我想知道/rootBash 提示中的目录在 Windows 中的什么位置?
我希望能够在 Bash 中编写也可以从 Windows 访问的文件
即-如果我这样做:
touch /root/foo
我在哪里可以访问foo我的电脑
bash windows-10 windows-subsystem-for-linux windows-10-v1607
我最近获得了 Windows 10 周年更新并安装了 bash。我尝试安装 nmap,但它无法运行。
我的问题:有没有办法让像 nmap 这样的命令在 WSL 中正确运行?
提前致谢!
运行时出现错误sudo nmap -sn 192.168.1.*:route_dst_netlink: cannot bind AF_NETLINK socket: Invalid argument
我记得在某处读到 WSL 不支持套接字,但可以使用 TCP。在这种情况下这可能吗?另外,WSL 会支持套接字吗?
networking bash windows-10 windows-subsystem-for-linux windows-10-v1607
ArchLinux (Manjaro)。
我正在运行一个 bash 文件。它运行 2 个进程(命令),使用&. 但是当我按Ctrl+C停止程序时 - 一个进程死了,另一个继续工作。
如何停止这两个进程?或者我如何编写一个新的脚本来杀死这两个进程?
我即将写一个shell脚本。通过将其颜色设置为黄色,我希望能够在视觉上将其某些输出文本区分为警告。
这是怎么做的?
我有一个包装脚本,它做了一些工作,然后将原始参数传递给另一个工具:
#!/bin/bash
# ...
other_tool -a -b "$@"
Run Code Online (Sandbox Code Playgroud)
这工作正常,除非“其他工具”在子shell中运行:
#!/bin/bash
# ...
bash -c "other_tool -a -b $@"
Run Code Online (Sandbox Code Playgroud)
如果我像这样调用我的包装脚本:
wrapper.sh -x "blah blup"
Run Code Online (Sandbox Code Playgroud)
然后,只有第一个原始参数 (-x) 被传递给“other_tool”。实际上,我没有创建子外壳,而是将原始参数传递给 Android 手机上的外壳,这应该没有任何区别:
#!/bin/bash
# ...
adb sh -c "other_tool -a -b $@"
Run Code Online (Sandbox Code Playgroud) bash ×10
linux ×3
shell ×2
sudo ×2
windows-10 ×2
windows-subsystem-for-linux ×2
cat ×1
macos ×1
networking ×1
path ×1
shell-script ×1
ssh ×1
tar ×1