这是一种可能性:
#!/bin/bash
die() {
echo >&2 "$@"
exit 1
}
arg="$1"
# Check that argument is valid (only 0's and 1's)
[[ $arg =~ ^[01]+$ ]] || die "Wrong argument \`$1'"
nargs=${#arg}
for ((i=1;i<=nargs;++i)); do
valuei=${arg:i-1:1}
# If value is 0, continue loop
((valuei)) || continue
# Execute script
echo "Executing subscript $i"
done
样本输出:
$ ./scriptname 10001
Executing script 1
Executing script 5
编辑。当您询问如何执行下标时,这是一种(完全有效的)可能性:
#!/bin/bash
# Maximum number of subscripts
nsub=5
die() {
echo >&2 "$@"
exit 1
}
arg="$1"
# Check that we have argument of correct length
(( ${#arg}==nsub )) || die "Must give argument of length $nsub"
# Check that argument is valid (only 0's and 1's)
[[ $arg =~ ^[01]+$ ]] || die "Wrong argument \`$1'"
for ((i=1;i<=nsub;++i)); do
# If value is 0, continue loop
((${arg:i-1:1})) || continue
# Execute script
source "${DOCROOT}/script$i.sh"
done