Solved

Checking if a word starts with the letter B

Posted on 2001-09-14
6
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 21

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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 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…

623 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