Solved

Checking if a word starts with the letter B

Posted on 2001-09-14
6
177 Views
Last Modified: 2010-04-21
I would like to check if the value I am testing starts with the letter B.I tried
 if [ ^$val ==?B? ]; then
         echo ?start with B?
else
        echo ?Does not start with B?
fi
But this does not work, it always goes to the else part even though the val is a string  starting with B

0
Comment
Question by:lubna132
6 Comments
 

Author Comment

by:lubna132
ID: 6483755
I tried if [ $val = ^B ] this does not work as well
0
 
LVL 11

Accepted Solution

by:
griessh earned 50 total points
ID: 6483847
try that for a ksh:

#This extracts the first character
variable = `echo $val | cut -c 1`

if [ "$variable" = "B" ] ; then
echo "starts with B"
else
echo "does not start with B"
fi

BTW: there are more cryptic ways to get that result :-)

======
Werner
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6484716
# works in any UNIX and in any shell
if [ `echo "$var"|grep ^B` ]; then
  echo match
else
  echo no match
fi
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:ecw
ID: 6486983
Bourne shell at its derivitives ...
 case "$var" in
   B*) echo "starts with a B" ;;
   *)  echo "does not start with a B" ;;
 esac
0
 
LVL 20

Expert Comment

by:tfewster
ID: 7774937
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation for this question in the Cleanup topic area as follows:
- Answered by griessh

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster (I don't work here, I'm just an Expert :-)
0
 

Expert Comment

by:SpideyMod
ID: 7864456
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

911 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

22 Experts available now in Live!

Get 1:1 Help Now