Solved

Unix Shell programming

Posted on 2004-10-28
271 Views
Last Modified: 2010-04-17
how can i get the reverse of a string which is stored in a variable given by the user in unix shell programming?
0
Question by:drunkenlogic
    4 Comments
     
    LVL 8

    Assisted Solution

    by:mugman21
    Drunkenlogic,

    Use the Rev command, there is an example at http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_10109991.html

    Mugman
    0
     
    LVL 3

    Assisted Solution

    by:vippx
    Hi there.. try this solution
    works !! :)

    #!/bin/bash

    WORD=$1

    let LENGTH=${#WORD}-1

    while [ "${n:=0}" -le "$LENGTH" ]
          do
                let pos="$LENGTH"-n      
                REVERSE="$REVERSE${WORD:pos:1}"
                let n+=1
          done

    echo $REVERSE

    Simple array manipulation

    0
     
    LVL 9

    Accepted Solution

    by:
    A simple korn shell :

    rev.sh
    -------
    #!/bin/ksh
    ZVSTRING="$1"
    ZRLENGTH=`printf $ZVSTRING | wc -c | awk '{print $1 }'`
    YRSTART=$ZRLENGTH
    while [ $YRSTART -gt 0 ]
    do
        YVCHR=`echo $ZVSTRING | cut -c$YRSTART`
        YOUTSTR="${YOUTSTR}${YVCHR}"
        (( YRSTART = YRSTART - 1 ))
    done
    print $YOUTSTR

    To use :
    smpd9:/oq/home1/n43125/junk>rev abcdefgh
    hgfedcba
    smpd9:/oq/home1/n43125/junk>

    Hope this would give you an idea!
    0
     
    LVL 9

    Expert Comment

    by:pratikroy
    Or you can use it as :

    smpd9:/oq/home1/n43125/junk>abc="abcdefgh"
    smpd9:/oq/home1/n43125/junk>rstr=`rev $abc`
    smpd9:/oq/home1/n43125/junk>echo $rstr
    hgfedcba
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This is an explanation of a simple data model to help parse a JSON feed
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    884 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

    17 Experts available now in Live!

    Get 1:1 Help Now