Solved

Checking if a word starts with the letter B

Posted on 2001-09-14
6
176 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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 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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

757 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