Solved

Bash - check the first letter of a var

Posted on 2007-04-06
4
216 Views
Last Modified: 2010-05-18
while [ -z $cnickname ] || echo ${cnickname:0:1} | grep [:digit:];
do
if echo ${cnickname:0:1} | grep [:digit:]; then
something

When i execute this it says: Syntax error: Bad substitution, any ideea whats wrong ?
The script should take the virst letter of $cnickname and see if its a digit, and if it is do something
0
Comment
Question by:rares_dumitrescu
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18863049
Are you sure you are running it in bash and not in sh?
0
 

Author Comment

by:rares_dumitrescu
ID: 18863093
you're right i was using /bin/sh, i changed it to #!/usr/local/bin/bash .. now it works but the if, or while does not turn on at echo ${cnickname:0:1} | grep [:digit:]; when i put something like '3blah'
0
 

Author Comment

by:rares_dumitrescu
ID: 18863113
worked
i know i might sound very stupid, but, why didn't it worked with /bin/sh and it works with /bin/bash ?
whats the difference ? (i am making some scripts on a server, like getegg getpsy etc (for irc) using dialog menu)
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 18863190
they are different shells, see
man sh
and
man bash
you might try grep '[0-9]'
or
grep '[[:digit:]]'
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now