Scripting
此内容尚不支持你的语言。
grfsh runs scripts written for bash as long as they stay within the features listed here. Run grfsh --check script.sh to find anything it cannot run before you try.
grfsh script.sh arg1 arg2grfsh -c 'echo "$1 and $2"' name first secondCommands and pipelines
Section titled “Commands and pipelines”make && echo built || echo failedls | grep -c rs! grep -q TODO notes.txt && echo clean{ echo one; echo two; } > both.txt( cd /tmp && pwd )time make$? holds the last status and PIPESTATUS holds the status of every stage of the last pipeline. set -o pipefail makes a pipeline fail when any stage fails.
Variables and parameters
Section titled “Variables and parameters”name=grfshecho "${name}" "${#name}" "${name:1:3}"echo "${missing:-default}" "${missing:=assigned}" "${name:+set}"echo "${path#*/}" "${path##*/}" "${file%.txt}" "${file%%.*}"echo "${text/old/new}" "${text//old/new}" "${text/#start/x}" "${text/%end/x}"echo "${name^}" "${name^^}" "${NAME,,}"echo "${text@Q}" "${text@U}" "${escaped@E}"ref=name; echo "${!ref}"echo "${!PREFIX_*}"Positional parameters work as in bash: $1, ${10}, $#, "$@", "$*", shift, set -- a b c and slices such as ${@:2} and ${@:2:3}. Special variables include $$, $!, $0, $-, $LINENO, $SECONDS, $RANDOM, $PPID, $PWD, $OLDPWD and $IFS.
Arrays
Section titled “Arrays”fruits=(apple banana cherry)fruits+=(date)echo "${fruits[1]}" "${fruits[-1]}" "${#fruits[@]}" "${!fruits[@]}"echo "${fruits[@]:1:2}"unset 'fruits[0]'
declare -A ages=([ana]=31 [bo]=27)ages[cy]=45for name in "${!ages[@]}"; do echo "$name ${ages[$name]}"; done
mapfile -t lines < file.txtread -ra words <<< "a b c"Indexed arrays are sparse, like bash.
Arithmetic
Section titled “Arithmetic”echo $((2 ** 10)) $((17 % 5)) $((x > 3 ? 1 : 0)) $((0xff)) $((2#1010))(( count++ ))(( total += size ))for (( i = 0; i < 3; i++ )); do echo "$i"; donedeclare -i n=5; n+=3Conditionals
Section titled “Conditionals”if [[ $file == *.rs && -f $file ]]; then echo rust; fi[[ $version =~ ^([0-9]+)\.([0-9]+) ]] && echo "major ${BASH_REMATCH[1]}"[ -d "$dir" ] || mkdir -p "$dir"test "$a" -lt "$b"
case $answer in y|yes) echo yes ;; n*) echo no ;; *) echo unknown ;;esacLoops and functions
Section titled “Loops and functions”for file in *.txt; do wc -l "$file"; donewhile read -r line; do echo "$line"; done < input.txtuntil ping -c1 host >/dev/null; do sleep 1; done
greet() { local who=${1:-world} echo "hi $who" return 0}greet thereFunction names may contain -, ., :, + and @, so names such as nvm_ls-remote work.
Expansion
Section titled “Expansion”echo {a,b}{1..2} {01..10} {a..z..5}echo ~ ~/src ~rootshopt -s globstar nullglob dotglob nocaseglobecho src/**/*.rsecho $(date +%Y) `whoami`diff <(sort a.txt) <(sort b.txt)tee >(gzip > out.gz) < input > /dev/nullWords are split on IFS after expansion. Double quotes prevent splitting and globbing, as in bash.
Redirections and here documents
Section titled “Redirections and here documents”cmd > out.txt 2> err.txtcmd >> log.txt 2>&1cmd &> all.txtcmd 2>/dev/nullexec 3> trace.log; echo hi >&3; exec 3>&-cat <<EOFHello $USEREOFcat <<-'EOF' no $expansion, leading tabs removed EOFgrep x <<< "here string"Options and traps
Section titled “Options and traps”set -euo pipefailset -xtrap 'rm -f "$tmp"' EXITtrap 'echo interrupted; exit 130' INTtrap 'echo "failed at line $LINENO"' ERRBuiltins worth knowing
Section titled “Builtins worth knowing”| Builtin | Notes |
|---|---|
printf |
%s %d %i %f %e %g %x %o %c %b %q %%, widths, precision, -v var |
read |
-r -a -n -t -s -d -p |
getopts |
standard option parsing with OPTARG and OPTIND |
declare, typeset, local |
-a -A -i -l -u -r -x -p |
mapfile, readarray |
-t -n -s |
type, command -v |
find out what a name resolves to |
ulimit, umask |
resource limits and file mode mask |
Run help inside grfsh for the full list and help name for one builtin.
What is missing
Section titled “What is missing”See Compatibility with bash for the full list. The most common gaps are extended globs (shopt -s extglob), name references (declare -n), coprocesses, select and let. grfsh --check reports each of them with its line number.