为什么模式*([[^ \/])在我的交互式外壳程序中有效但在脚本中无效? [英] Why does the pattern *([^\/]) work in my interactive shell but not a script?
本文介绍了为什么模式*([[^ \/])在我的交互式外壳程序中有效但在脚本中无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个脚本,使用bash参数替换将工作目录剥离到有限的长度.它可以在命令行上运行,但是相同的替换在脚本中什么也没做.
I'm writing a script to strip a working directory to a limited length using bash parameter substitution. It works on the command line, but the same substitution does nothing in the script.
代码:
#!/bin/bash
# Limit of working directory length
LIMIT=10
dir=${1/#$HOME/\~}
# If it's too long, normalize it by stripping ~ and adding ...
if [ ${#dir} -gt $LIMIT ]; then
dir=${dir/#\~/"..."}
fi
echo $dir
# Strip levels until short enough or can't strip anymore.
while [[ ( ${#dir} -gt $LIMIT ) && ( "$dir" != "$last_dir" ) ]]; do
last_dir="$dir"
# Strip a level off.
dir=${dir/#...\/*([^\/])/"..."} <- broken line
echo $dir
done
echo $dir
如果我愿意
test=".../School/CS352/Project1"
text=${test/#...\/*([^\/])/"..."}
我得到了.../CS352/Project1
,这就是我想要的.但是同一个sub在我的脚本中什么也没做.
I get .../CS352/Project1
, which is what I want. But the same sub does nothing in my script.
问题:如何使代码中的标记行表现得像上面的示例一样?
推荐答案
运行:
shopt -s extglob
...打开此支持.
这篇关于为什么模式*([[^ \/])在我的交互式外壳程序中有效但在脚本中无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文