0

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.

The code:

#!/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

If I do

test=".../School/CS352/Project1"
text=${test/#...\/*([^\/])/"..."}

I get .../CS352/Project1, which is what I want. But the same sub does nothing in my script.

Question: How do I make the marked line in the code behave like the example above?

4

1 回答 1

1

跑:

shopt -s extglob

...打开此支持。

于 2014-11-04T01:40:25.277 回答