User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 402,521 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,676 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 444 | Replies: 2
Reply
Join Date: Mar 2008
Posts: 19
Reputation: sjgriffiths is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
sjgriffiths sjgriffiths is offline Offline
Newbie Poster

Variable check

  #1  
May 23rd, 2008
Is there any way to check the first $NUM chars of a variable?

For example if I have a veriable which contains

VAR="LONGMORE LTD"

Can i check the first character begins with L?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Posts: 51
Reputation: radoulov is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: Variable check

  #2  
May 23rd, 2008
With ksh93, bash, zsh:

[[ $VAR == L* ]]&&echo OK||echo KO

For old shells use case:

case $VAR in L*) echo OK;;*)echo KO;;esac
Reply With Quote  
Join Date: Oct 2007
Posts: 267
Reputation: eggi is on a distinguished road 
Rep Power: 1
Solved Threads: 26
eggi eggi is offline Offline
Posting Whiz in Training

Re: Variable check

  #3  
May 23rd, 2008
Hey there,

If you need to be more generic (like sometimes you want to match the first 3 letters and sometimes 2, etc) you can use expr's : (colon) operator to do string and substring matching.

Hope that helps,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 6:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC