The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use substr to return data into a shell script variable? ttunell Shell Programming and Scripting 3 07-23-2008 01:02 AM
Substr in shell script gjithin Shell Programming and Scripting 7 05-10-2008 05:40 PM
substr() thru awk Korn Shell Script sbryant Shell Programming and Scripting 4 01-24-2008 11:16 AM
shell script string extension vbm Shell Programming and Scripting 4 11-10-2006 01:58 AM
Trouble using substr function with Bourne shell script E2004 Shell Programming and Scripting 4 09-29-2005 11:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-05-2008
yajaykumar yajaykumar is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 8
substr from a string in Shell script

Shell Scripting Gurus,
I am having a hard time trying to figure out what I am doing wrong in my script. Below is the script snippet. It gives an error when it tries to execute the expression.

a=`expr substr $stringZ 5 10`


#!/bin/bash
echo "Hello"
stringZ="abcABC123ABCabc"
echo $stringZ
echo "Length is ${#stringZ}"
echo `expr "$stringZ" : '.*'`
a=`expr substr $stringZ 5 10`
echo $a

output :

Hello
abcABC123ABCabc
Length is 15
15
expr: syntax error

$


What am I doing wrong? Why is it giving syntax error when it executes
a=`expr substr $stringZ 5 10`

Can one of you gurus out there help me fix this?

Thanks a lot!
Ajay.
  #2 (permalink)  
Old 08-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Dunno why you get the syntax error from expr, but you could use bash's builtin substring facility.


Code:
a=${stringZ:5:10}

  #3 (permalink)  
Old 08-05-2008
buffoonix buffoonix is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 145
I think this should rather have been addressed by era,
the herder of Randal's useless use of cat awards?
I would here say, contender for useless use of echo and backticks award
Quote:
echo `expr "$stringZ" : '.*'`
But I noticed one difference.
The nifty bash substr expansion seems to start counting chars from naught
(which seems logical to programmers)
whereas the expr substr seems indexing from 1 (which I would say is more Fortran style, if I remember correctly)

Code:
$ str=abcABC123ABCabc
$ echo ${str:5:10}
C123ABCabc
$ expr substr $str 5 10
BC123ABCab

  #4 (permalink)  
Old 08-06-2008
yajaykumar yajaykumar is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 8
Thanks a lot!!!

Era and Buffoonix,

Thanks a lot! Your inputs helped me in fixing my problem.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0