代码: 全选
1 #! /usr/bin/bash
2 myfruit="pear"
3 #当myfruit没有赋值时,apple的值取代myfruit
4 #myfruit的值为pear
5 fruit=${myfruit:-apple}
6 echo when myfruit is set,fruit is:$fruit
7 #清空myfruit,myfruit的值为apple
8 unset myfruit
9 fruit=${myfruit:-apple}
10 echo when myfruit is unset,fruit is:$fruit
11
12 #将错误信息打印,并终止代码后面的操作
13 #unset var_x
14 #${var_x:?"the var_x is undefined"}
15
16 #若var_x有值,word取代var_x的值, 但var_x的值不变
17 var_x="beijing 2008";
18 echo ${var_x:+"aoyun beijing"}
19 echo var_x is :$var_x
20 unset var_x
21 echo ${var_x:+"aoyun beijing"}
22 echo var_x is:$var_x
23
24 #变量替换
25 file=/dir1/dir2/dir3/my.file.txt
26 echo ${file/dir/path}
第一次学shell,首先我按照网上的一些错误分析进行了修改。我的系统支持bash和sh两种,我也曾修改成为#! /usr/bin/sh来执行,还是报同样的错误。when myfruit is set,fruit is:pear
when myfruit is unset,fruit is:apple
aoyun beijing
var_x is :beijing 2008
var_x is:
./varset.sh: 26: Bad substitution
各位shell大神,请问一下这种错误应该怎么解决?