Solved

Checking if a word starts with the letter B

Posted on 2001-09-14
6
181 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

751 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