- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于一项作业,我应该创建一个名为 my_which.sh
的脚本,它将“做与 Unix 命令相同的事情,但使用 for 循环而不是 if”。我也不允许在我的脚本中调用 which。
我对此是全新的,并且一直在阅读教程,但我对如何开始感到很困惑。 which
不就是列出命令的路径名吗?
如果是这样,我将如何在不调用 which 的情况下使用 for 循环和 if 语句来显示正确的路径名?
例如,如果我运行我的脚本,它将echo %
并等待输入。但是,我该如何将其转化为查找目录呢?所以它看起来像这样?
#!/bin/bash
path=(`echo $PATH`)
echo -n "% "
read ans
for i in $path
do
if [ -d $i ]; then
echo $i
fi
done
我将不胜感激任何帮助,甚至任何可以帮助我开始这方面的入门教程。老实说,我很困惑应该如何实现。
最佳答案
安全地拆分您的PATH
变量。这是在分隔符处拆分字符串的通用方法,对于任何可能的字符(包括换行符)都是 100% 安全的:
IFS=: read -r -d '' -a paths < <(printf '%s:\0' "$PATH")
我们人为添加了:
,因为如果PATH
以尾随:
结尾,则可以理解当前目录应该在中路径
。虽然这是危险的并且不推荐,但如果我们想模仿 which
,我们也必须考虑到这一点。如果没有这个尾随冒号,PATH
像 /bin:/usr/bin:
将被分成
declare -a paths='( [0]="/bin" [1]="/usr/bin" )'
而尾随冒号的结果数组是:
declare -a paths='( [0]="/bin" [1]="/usr/bin" [2]="" )'
这是其他答案遗漏的一个细节。当然,只有当 PATH
已设置且非空时,我们才会这样做。
通过拆分 PATH
,我们将使用 for 循环来检查是否可以在给定目录中找到参数。请注意,仅当参数不包含 /
字符时才应执行此操作!这也是其他答案遗漏的东西。
我的版本
处理一个独特的选项-a
,打印每个参数的所有匹配路径名。否则,只打印第一个匹配项。我们也必须考虑到这一点。
我的版本处理以下退出状态:
0 if all specified commands are found and executable
1 if one or more specified commands is nonexistent or not executable
2 if an invalid option is specified
我们也会处理。
我猜下面的代码相当忠实地模仿了我的 which
的行为(而且它是纯 Bash):
#!/bin/bash
show_usage() {
printf 'Usage: %s [-a] args\n' "$0"
}
illegal_option() {
printf >&2 'Illegal option -%s\n' "$1"
show_usage
exit 2
}
check_arg() {
if [[ -f $1 && -x $1 ]]; then
printf '%s\n' "$1"
return 0
else
return 1
fi
}
# manage options
show_only_one=true
while (($#)); do
[[ $1 = -- ]] && { shift; break; }
[[ $1 = -?* ]] || break
opt=${1#-}
while [[ $opt ]]; do
case $opt in
(a*) show_only_one=false; opt=${opt#?} ;;
(*) illegal_option "${opt:0:1}" ;;
esac
done
shift
done
# If no arguments left or empty PATH, exit with return code 1
(($#)) || exit 1
[[ $PATH ]] || exit 1
# split path
IFS=: read -r -d '' -a paths < <(printf '%s:\0' "$PATH")
ret=0
# loop on arguments
for arg; do
# Check whether arg contains a slash
if [[ $arg = */* ]]; then
check_arg "$arg" || ret=1
else
this_ret=1
for p in "${paths[@]}"; do
if check_arg "${p:-.}/$arg"; then
this_ret=0
"$show_only_one" && break
fi
done
((this_ret==1)) && ret=1
fi
done
exit "$ret"
为了测试一个参数是否可执行,我正在检查它是否是一个常规文件1,它是可执行的:
[[ -f $arg && -x $arg ]]
我猜这与我的 which
的行为很接近。
1 正如@mklement0 指出的(谢谢!)-f
测试,当应用于符号链接(symbolic link)时,测试符号链接(symbolic link)的目标.
关于bash - 在 bash 脚本中创建 which 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28642253/
我用 IntelliJ IDEA 2021.1 CE 在 流行!_OS 20.04 与 bash 5.0.17 . 问题造句:我将IntelliJ终端设置为/bin/bash通过 IntelliJ 设
给定如下命令: bash --shortcuts 我想显示一个快捷方式列表,就像在这个页面上一样: http://www.skorks.com/2009/09/bash-shortcuts-for-m
我有一个脚本可以操作数据、创建参数并将它们发送到第二个脚本。其中一个参数包含一个空格。 脚本1.sh: args=() args+=("A") args+=("1 2") args+=("B") .
我的脚本的“只运行一次”版本的一个非常简单的示例: ./myscript.sh var1 "var2 with spaces" var3 #!/bin/bash echo $1 #output: va
我想了解数字( double )在 bash 中是如何表示的,以及当我在 bash 中以十六进制格式打印数字时会发生什么。 根据 IEEE 754 标准,double 应由 64 位表示:52 位(1
我试图在 bash -c "..." 命令中获取 bash 脚本,但它不起作用。 如果我在 bash -c "..." 之外运行命令,它会起作用。 我需要使用 bash -c "..." 因为我想确保
如何检测我的 bash shell 中是否加载了 bash 补全包?从 bash-completion 的 2.1 版(包含在 Debian 8 中)开始,除了 BASH_COMPLETION_COM
我的 bash_profile 中有一个投影函数。现在我试图从 bash 脚本中调用这个函数,但是我得到了一个未找到的错误。如何使投影函数对 bash 脚本可见? 最佳答案 必须导出函数 export
我正在编写一个 bash 脚本,它接受许多命令行参数(可能包括空格)并通过登录 shell 将它们全部传递给程序 (/bin/some_program)。从 bash 脚本调用的登录 shell 将取
当我创建一个新的 bash 进程时,提示符默认为一个非常简单的提示符。我知道我可以编辑 .bashrc 等来更改它,但是有没有办法使用 bash 命令传递提示? 谢谢! 最佳答案 提示由 PS1、PS
好的,我希望这个问题有一定道理,但是 bash shell 和 bash 终端之间有什么区别?例子。当我第一次打开终端时,会提示我当前的目录和用户名。在终端窗口标题中显示 -bash- ,当我键入 e
我是 SBCL 的新手,我正在尝试从 bash 终端运行存储在文本文件中的 Lisp 脚本。 这是我在文件开头写的内容 http://www.sbcl.org/manual/#Running-from
我知道我们可以在 bash 中使用将十六进制转换为十进制 #!/bin/bash echo "Type a hex number" read hexNum echo $(( 16#$hexNum ))
我正在尝试在 bash 脚本中自动完成文件夹名称。如果我输入完整的文件夹名称,一切正常,但我不知道如何自动完成名称。有什么想法吗? repo() { cd ~/Desktop/_REPOS/$1 }
我想检查远程网站上的一些文件。 这里是bash命令生成计算文件md5的命令 [root]# head -n 3 zrcpathAll | awk '{print $3}' | xargs -I {}
是否有任何内置函数可以使用 bash shell 脚本从给定日期获取下周日(下周一、下周二等)?例如,2014 年 9 月 1 日之后的第一个星期日是什么时候?我预计 2014 年 9 月 7 日。
我一直在尝试根据表格重命名一些特定文件,但没有成功。它要么重命名所有文件,要么给出错误。 该目录包含数百个以长条形码命名的文件,我只想重命名包含模式 _1_ 的文件。 例子 barcode_1_bar
bash 中有没有办法用变量的内容替换文本文件中的占位符? 例如,我想发送一封电子邮件通知,如下所示: Dear Foo, Alert: blah blah blah blah blah blah
我有一个 bash 脚本,它在某些字符串上附加了一个重音字符,导致它失败,我找不到这些字符在哪里或如何进入那里。 这是一些示例输出: mv: cannot move â/tmp/myapp.zipâ
这个问题在这里已经有了答案: How do I place stdout on edit line? (1 个回答) Can a bash script prepopulate the prompt
我是一名优秀的程序员,十分优秀!