Results 1 to 3 of 3
Hello,
I've started writing bash-scripts, at the moment I try to write a part in which a first digit of a number is checked for containing correct values.
I did ...
- 05-27-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 2
[SOLVED] bad substitution problem (bash)
Hello,
I've started writing bash-scripts, at the moment I try to write a part in which a first digit of a number is checked for containing correct values.
I did this
dummy=${value:0:1}
if [ -z ${dummy##*[^0-6]*} ]; then
invalid=1
fi
to check if the first digit of an entry is between 0-6. It works fine this way. My question is how it is possible to integrate the ${value:0:1} into the if clause so I don't need the dummy variable anymore. I've tried several times, but I always receive an "bad substitution" error. This should be possible, shouldn't it?
Thanks a lot for any help!
== Solution found ==
OK, I finally found out myself...
it can be done via
if [ `expr match "$value '[0-6]*'` = 0 ]
since match checks by default the first digit.
- 05-30-2008 #2
Though using expr match is an efficient way....

Code:[/E*Fare/Users/qabuild/aksutil/pumba] $ a=1-JholaLinux [/E*Fare/Users/qabuild/aksutil/pumba] $ b=JholaLinux-5 [/E*Fare/Users/qabuild/aksutil/pumba] $ c=7JholaLinux [/E*Fare/Users/qabuild/aksutil/pumba] $ d=JholaLinux-9 [/E*Fare/Users/qabuild/aksutil/pumba] $ echo $a - $b - $c - $d 1-JholaLinux - JholaLinux-5 - 7JholaLinux - JholaLinux-9
Code:[/E*Fare/Users/qabuild/aksutil/pumba] $ if [ `echo ${a:0:1}|grep [0-6]` ];then echo "Timon"; else echo "Nahin Pumba"; fi Timon [/E*Fare/Users/qabuild/aksutil/pumba] $ if [ `echo ${b:0,1}|grep [0-6]` ];then echo "Timon"; else echo "Nahin Pumba"; fi Timon [/E*Fare/Users/qabuild/aksutil/pumba] $ if [ `echo ${c:0:1}|grep [0-6]` ];then echo "Timon"; else echo "Nahin Pumba"; fi Nahin Pumba [/E*Fare/Users/qabuild/aksutil/pumba] $ if [ `echo ${d:0,1}|grep [0-6]` ];then echo "Timon"; else echo "Nahin Pumba"; fi Nahin Pumba
- 05-30-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 2
Thanks, I'll keep that in mind, too - I have just recently learned what | is for in such a context


Reply With Quote
