shell脚本Bad substitution错误

sh/bash/dash/ksh/zsh等Shell脚本
回复
raokings
帖子: 7
注册时间: 2009-08-29 8:25

shell脚本Bad substitution错误

#1

帖子 raokings » 2012-01-04 11:13

代码: 全选

  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}
运行时错误为(红色字体为错误):
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,首先我按照网上的一些错误分析进行了修改。我的系统支持bash和sh两种,我也曾修改成为#! /usr/bin/sh来执行,还是报同样的错误。
各位shell大神,请问一下这种错误应该怎么解决?
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: shell脚本Bad substitution错误

#2

帖子 eexpress » 2012-01-04 11:20

echo ${file/dir/path}
这是要干嘛。

echo $file/dir/path ?
● 鸣学
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: shell脚本Bad substitution错误

#3

帖子 枫叶饭团 » 2012-01-04 12:06

echo ${file/dir/path}
看半天,没想明白这是什么意思...删掉吧 :em04
raokings
帖子: 7
注册时间: 2009-08-29 8:25

Re: shell脚本Bad substitution错误

#4

帖子 raokings » 2012-01-04 12:33

eexpress 写了:echo ${file/dir/path}
这是要干嘛。

echo $file/dir/path ?

变量内容的替换
${value/pattern/string}
${value//pattern/string}
进行变量内容的替换,把与pattern匹配的部分替换为string的内容,/与//的区别与上同

教程上是这样写的。。我想替换以后输出
raokings
帖子: 7
注册时间: 2009-08-29 8:25

Re: shell脚本Bad substitution错误

#5

帖子 raokings » 2012-01-04 12:40

枫叶饭团 写了:echo ${file/dir/path}
看半天,没想明白这是什么意思...删掉吧 :em04

3.若变量a="beijing 2008",请用变量内容替换的方法将beijing更换为paris,2008替换为2012
介个是教程的练习题 是不是我的写法不对?
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: shell脚本Bad substitution错误

#6

帖子 eexpress » 2012-01-04 12:45

替换哦。
你这是练习题目吧。我说没看懂干嘛。

你这写法,没啥问题啊。

你应该这样来
bash xxxxx.bash
这就强制使用bash了。
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: shell脚本Bad substitution错误

#7

帖子 eexpress » 2012-01-04 12:47

● echo $SHELL
可以看自己的当前shell

系统缺省是dash
● 鸣学
raokings
帖子: 7
注册时间: 2009-08-29 8:25

Re: shell脚本Bad substitution错误

#8

帖子 raokings » 2012-01-04 12:57

eexpress 写了:替换哦。
你这是练习题目吧。我说没看懂干嘛。

你这写法,没啥问题啊。

你应该这样来
bash xxxxx.bash
这就强制使用bash了。

:em06 恩 强制bash了就对拉
那是不是有以后都要这样用才不会报错呢
raokings
帖子: 7
注册时间: 2009-08-29 8:25

Re: shell脚本Bad substitution错误

#9

帖子 raokings » 2012-01-04 13:04

eexpress 写了:● echo $SHELL
可以看自己的当前shell

系统缺省是dash

我当前缺省值就是
root@ubuntu:/var/www/bash# echo $SHELL
/bin/bash
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: shell脚本Bad substitution错误

#10

帖子 eexpress » 2012-01-04 14:32

那就是你第一句没写好

代码: 全选

#!/bin/bash
另外,替换里面,可以使用变量的。
● 鸣学
头像
GoodLuckyBoy
帖子: 50
注册时间: 2010-04-23 17:11

Re: shell脚本Bad substitution错误

#11

帖子 GoodLuckyBoy » 2015-06-19 14:39

最近在写脚本的时候也遇到这个问题耶
脚本在电脑上执行的时候没有问题,但放到开发板上执行的时候会出错

代码: 全选

line 90: syntax error: bad substitution
原来是开发板上的 sh 不支持数组的这种写法

代码: 全选

${array[@]} #或是 ${array[*]} 
只好使用下面这种写法了

代码: 全选

"$array"
每夜唱不停,为了心中那点未了情
回复