Solved

Bash - check the first letter of a var

Posted on 2007-04-06
4
217 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

773 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