Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    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




    Quote Originally Posted by UBB2 View Post
    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.
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  3. #3
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...