LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-16-2004, 03:26 PM   #1
dpressm
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Rep: Reputation: 0
shell question variable substitution


I can do this in two statements but I wanted to do so in 1:

a='"apr 04"'
echo $a
curr_yr="${a#?????}"
curr_yr="FY${curr_yr%?}"
echo $curr_yr

yields my desired result FY04 in otherwords I want to strip the leading "and next three characters and the last character and then prepend FY .

why does not

curr_yr="FY${${a#?????}%?}"

work????
 
Old 11-16-2004, 05:06 PM   #2
dpressm
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Is my question clear?

Hi I am the originator of this thread and new to this forum and to shell scripting. I am amazed at how quickly my thread has accumulated 25 viewings but suprised that there have been no replies.

Please I am not complaining just wondering if my posting was not understandable, off topic or too juvenile (My wife would vote for the latter no matter what the question) for this forum.

Thanks!
 
Old 11-16-2004, 07:29 PM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
How about using substrings like this instead?

echo ${a:5:2}


To see why the method you used doesn't work, try expanding it as the shell would:

curr_yr="FY${${a#?????}%?}"

gives

curr_yr="FY${04"%?}"
 
Old 11-16-2004, 08:15 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Is this more like what you have in mind?

#!/bin/bash

a=`date '+%b%y'`
echo $a
echo "FY${a:3}"
 
Old 11-17-2004, 06:06 AM   #5
dpressm
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Original Poster
Rep: Reputation: 0
I get a bad substitution error with ${a:3:2}

I get a bad substitution error with ${a:3:2} that prompted me to use the arcane method I went to. I am using Sun (version in coming post) Solaris and ksh and all I read says it should work.

I do not understand why.

Also thank you for the posting regarding how the shel would do the first substitution - I guess I need to use single or back quotes in the outer set???
 
Old 11-17-2004, 06:27 AM   #6
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Sorry, I just assumed bash in the absence of any other information... I'm not familiar with the korn shell, especially not on Solaris, but I imagine expansion works in a similar way (especially since I tried your examples in bash, and they seemed to work)

I don't think adding quotes will help, because the form is like this:

${varname%pattern}

shell expansion's not designed to work with constant values, so when the inner block is expanded, it doesn't correspond to a valid variable name any more - just a constant string. Is it really so bad to do it in two lines, using a throwaway variable? A man page for ksh I found on the net doesn't list the substring syntax, so it's possible your version doesn't support that.

Another possible option would be passing the string to another tool, like cut: result=`echo $a | cut -c 6-7`
 
Old 11-17-2004, 07:34 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
The syntax ${parameter:offset:length} is a bash specific extension, and doesn't appears on ksh man pages, wether it runs on Solaris or not.
You have the option of either use bash on Solaris or use a portable way, e.g.:
Code:
$(expr $parameter : ".\{$offset\}\(.\{$length\}\)")
 
Old 11-17-2004, 03:32 PM   #8
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I found the substring syntax listed on a page for "bash, csh, and ksh scripting". It never mentioned that ksh didn't support it, and even implied that it was a portable method! Very misleading....
 
Old 11-18-2004, 01:05 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
I found the substring syntax listed on a page for "bash, csh, and ksh scripting".
Where is this page located ?
 
Old 11-18-2004, 07:00 AM   #10
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
http://www.mkssoftware.com/docs/man1/sh.1.asp

Since I wasn't interested in the description of bash or ksh, I skipped on down to the substring section. That will teach me to read more carefully - it is actually a man page for a bash/ksh compatible shell for Windows (of all things)! I'm sure this was not the only page though, as there was another one that definitely had "scripting" in the title... I assume it was something similar (perhaps even a scripting guide on the same site).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Random Variable Daniel Programming 14 12-03-2007 05:00 AM
Variable substitution (stupid noob) question in tcsh King of Men Linux - Newbie 4 12-04-2005 09:12 AM
Export Shell Variable not working maxhugen Linux - Newbie 6 01-06-2005 10:12 PM
[c shell] variable question saiz66 Programming 5 09-29-2004 09:45 AM
parsing a variable using linux shell philco Programming 6 09-04-2003 06:52 PM


All times are GMT -5. The time now is 11:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration