Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unix Shell programming

Posted on 2004-10-28
4
Medium Priority
?
305 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
Comment
Question by:drunkenlogic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:mugman21
mugman21 earned 200 total points
ID: 12432022
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
vippx earned 360 total points
ID: 12432500
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:
pratikroy earned 440 total points
ID: 12433670
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
ID: 12433688
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

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

610 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