LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 05-05-2010, 03:19 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 154

Rep: Reputation: 15
how to solve ${path:1:4} problem


I have following in mu script.
Code:
path=/test/test1
echo ${path:1:4}
It gives "test" as output in Linux. But fails in solaris and gives error "bad substitution"

Please help me

ThanQ,
Nagendra.
 
Old 05-05-2010, 03:29 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Which bash (ksh??) version are you using?

I'm not sure if parameter expansion is supported by the version of bash/ksh that comes with solaris. Check the manpage on the sun box.

If it is not supported you need to work around it using other tools available (sed/cut/awk/..).

Hope this helps.
 
Old 05-05-2010, 06:27 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
This syntax is unsupported by ksh88 but works with ksh93 (might be available as dtksh on Solaris) and bash.
 
Old 05-07-2010, 03:23 AM   #4
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 154

Original Poster
Rep: Reputation: 15
I write one function to solve this problem


Code:
cutstring(){
        if [ $2 -lt 0 ]
        then
                echo $1
                return
        fi
        if [ $3 -eq 0 ]
        then
                return
        elif [ $3 -lt 0 ]
        then
                echo "$3: substring expression < 0"
                return
        fi

        count=0
        numchar=1
        char=`expr $2 + 1`
        while :
        do
                if [ $count -eq "$2" ]
                then
                        a=`echo $1 | cut -b$char`
                        b=``$b``$a``
                        if [ $numchar-eq $3 ]
                        then
                                echo $b
                                break
                        else
                                char=`expr $char + 1`
                                numchar=`expr $numchar + 1`
                        fi
                else
                        count=`expr $count + 1`
                fi
       done
}

str=`cutstring "abcdef" 1 4`
echo $str


output of this : bcde



ThanQ,
Nagendra
 
Old 05-07-2010, 03:48 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
It certainly can be optimized/simplified:

Code:
echo ${path:1:4}
can be converted to this which will work with all shells and Unix releases:
Code:
echo "$path" | cut -c2-5
 
  


Reply



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
solve my problem kamalkirat1 Linux - Software 2 12-03-2008 07:47 AM
could u solve my problem chandu.bezawada Linux - General 3 07-26-2007 06:11 AM
I need help to solve this problem TheTiger Fedora 1 10-26-2005 01:01 PM
help..how i can solve this problem?// TAAN Linux - General 2 02-16-2005 07:53 AM
No one can solve my problem Sundance Linux - Newbie 5 12-08-2003 01:13 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration