Fedora Linux Support Community & Resources Center
  #1  
Old 14th November 2011, 09:02 AM
menha Offline
Registered User
 
Join Date: Nov 2011
Posts: 3
windows_xp_2003firefox
Shell script to accept any input and reverses the order

Dear All I am trying to create a shell script that accepts any input and reverses the order. I came up with the code as below but however I don't seem to achieve my objective. Please members kindly tell me what problem is with my script


!/bin/sh
#
#
clear

#
#
echo -e "\n\n\nFor Example: Given 1234, I will print 4321.\n\n"

# echo -e "\n\nPlease enter a string of numbers:\n"

# read num
set num=1234
set rev=0
set tmp_num="$num"

while [ $num !=$rev ]
do

RMND=$num %10
rev=$RMND +10*$rev
num=$num / 10
done
echo -e "\nThe reverse of the Number $tmp_num is $rev"
Reply With Quote
  #2  
Old 14th November 2011, 09:42 AM
Gödel's Avatar
Gödel Offline
Registered User
 
Join Date: Jul 2009
Location: London,England
Posts: 1,095
linuxfirefox
Re: Shell script to accept any input and reverses the order

your shebang on line 1 is missing the #, and you can just use 'rev' to reverse a string

Code:
#!/bin/sh
#
clear

#
echo -e "\n\n\nFor Example: Given 1234, I will print 4321.\n\n"

# echo -e "\n\nPlease enter a string of numbers:\n"
# read num
num=1234

echo -ne "\nThe reverse of the Number $num is "
echo "$num" | rev


---------- Post added at 09:39 AM ---------- Previous post was at 09:38 AM ----------

if rev isn't available do 'yum install util-linux-ng'

---------- Post added at 09:42 AM ---------- Previous post was at 09:39 AM ----------

if you really just want pure shell code, see (for example) http://www.unix.com/shell-programmin...ll-script.html
Reply With Quote
  #3  
Old 14th November 2011, 10:09 AM
menha Offline
Registered User
 
Join Date: Nov 2011
Posts: 3
windows_xp_2003firefox
Re: Shell script to accept any input and reverses the order

Thanks, I'm on it will give you feed back

---------- Post added at 03:39 PM ---------- Previous post was at 03:14 PM ----------

Dear Godel, the code you gave me works pretty fine im grateful.

I however would like to add some more functionality such that I can be able to enter from the keyboard a list of input such as peoples names or figures with 2 decimal places in any order and the order in which they are entered is reversed. For instance i enter 20 30 40 from the keyboard then the out put is 40 30 30 or ABCD EFGH IJKL and the out put is IJKL EFGH ABCD
Reply With Quote
  #4  
Old 14th November 2011, 11:17 AM
Gödel's Avatar
Gödel Offline
Registered User
 
Join Date: Jul 2009
Location: London,England
Posts: 1,095
linuxfirefox
Re: Shell script to accept any input and reverses the order

well, there are many ways to do it (splitting the string into tokens), something like this should work

Code:
#!/bin/sh
#
clear

#
echo -e "\n\n\nFor Example: Given 1234 5678 abcd, I will print 4321 8765 dcba.\n\n"

#echo -e "\n\nPlease enter a string:\n"
#read string
string="1234 5678 abcd"

echo -ne "\nThe reverse of the string $string is "
revstring=""
for i in $(echo $string)
do
  tmpstr=$(echo "$i" | rev)
  revstring="$revstring$tmpstr "
done
echo "$revstring"
Reply With Quote
  #5  
Old 14th November 2011, 11:46 AM
menha Offline
Registered User
 
Join Date: Nov 2011
Posts: 3
windows_xp_2003firefox
Re: Shell script to accept any input and reverses the order

Thanks again, one of my first serious encounters with shell has been quit an experience. Any Ideas of where I can get material to introduce me to detailed shell scripting from scratch to detailed and Advanced would be appreciated. Thanks again
Reply With Quote
  #6  
Old 14th November 2011, 12:43 PM
Gödel's Avatar
Gödel Offline
Registered User
 
Join Date: Jul 2009
Location: London,England
Posts: 1,095
linuxfirefox
Re: Shell script to accept any input and reverses the order

maybe have a look at these bash tutorials (bash is the most popular shell in modern linux)

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

http://linuxconfig.org/Bash_scripting_Tutorial

http://tldp.org/LDP/abs/html/

---------- Post added at 12:43 PM ---------- Previous post was at 12:38 PM ----------

a slight simplification of the reverse script is
Code:
string="1234 5678 abcd"

echo -ne "\nThe reverse of the string $string is "
for i in $(echo $string)
do
echo "$i" | rev | tr '\n' ' '
done
echo
Reply With Quote
Reply

Tags
accept, input, order, reverses, script, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fedora12 + arm-linux-insight: target port doesn't accept input webman Using Fedora 1 11th April 2010 11:35 AM
Texmaker won't accept input from keyboard on second editing pass kalwisti Using Fedora 8 18th June 2009 12:29 AM
call remote shell script within an expect script PhillyFloyd Programming & Packaging 2 16th October 2007 10:29 PM
starting a shell script inside a php script gw348 Using Fedora 6 23rd April 2007 09:30 PM


Current GMT-time: 10:06 (Tuesday, 21-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat