The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help needed for writing a script sriram_1978 Shell Programming and Scripting 4 12-06-2007 04:10 AM
Writing the script jess_t03 Shell Programming and Scripting 1 10-10-2007 06:17 AM
Help needed in writing awk script for xml source naren_0101bits Shell Programming and Scripting 2 09-24-2007 02:07 AM
help writing script dr46014 Shell Programming and Scripting 8 08-27-2007 05:08 AM
plz help in writing awk script LAKSHMI NARAYAN Shell Programming and Scripting 3 07-14-2007 04:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 06-11-2008
wrapster wrapster is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 193
needed help in writing a script!

Hi all,

I needed help in writing a script for the following question.please help.

Non-recursive shell script that accepts any number of arguments and prints
them in the Reverse order. (For example, if the script is named rargs, then
executing rargs A B C should produce C B A on the standard output).

Thanks
  #2 (permalink)  
Old 06-11-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Why can't we use recursion?


Code:
#!/bin/sh

case $# in
  0|1) echo "$@";;
  *) arg=$1; shift; exec "$0" "$@" "$arg"
esac

  #3 (permalink)  
Old 06-12-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
... Actually that's pretty broken, but you get the idea (-:
  #4 (permalink)  
Old 06-12-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool This sort-of seemed like a game/challenge

Without any recursive programming was an interesting twist. But, I figured there had to be "some" reason for this requirement.

Anyway, I assumed by print in reverse order you meant the reverse of entry order. Saying that, perhaps the following script:


Code:
#! /bin/bash
#shift_input

tmp_file="shift_value_temp"
tmp_file2="shift_value_temp2"
tmp_file3="shift_value_temp3"
tmp_file4="shift_value_temp4"
rm $tmp_file 2>/dev/null
rm $tmp_file2 2>/dev/null
rm $tmp_file3 2>/dev/null
rm $tmp_file4 2>/dev/null

echo "$*" >$tmp_file
cat $tmp_file | tr " " "\n" >$tmp_file2
tac $tmp_file2 >$tmp_file3
cat $tmp_file3 | tr "\n" " ">$tmp_file4

cat $tmp_file4
echo " "


Code:
> shift_input a b c g h i d e f
f e d i h g c b a  
>

Perhaps could be done without the four work files, but I needed to write the data somewhere as I thought about the steps.
  #5 (permalink)  
Old 06-12-2008
spirtle spirtle is offline
Registered User
  
 

Join Date: Jun 2008
Location: Scotland
Posts: 150
Here's one in bash

Code:
while [ $# -gt 0 ]
do
  rev="$1 $rev"
  shift
done
echo $rev

and in Perl

Code:
while(@ARGV)
{
  unshift @rev, shift @ARGV;
}
print join ' ', @rev, "\n";

  #6 (permalink)  
Old 06-13-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
For the Perl version, you can just say reverse @ARGV -- no need to code the loop by hand.


Code:
#!/bin/sh
exec perl -le '$, = " "; print reverse @ARGV' "$@"

By manipulating $, you can avoid the explicit join.

joeyg's orgy in temp files and Useless Use of Cat (no offense ...) can be simplified radically:


Code:
echo "$@" | tr -s " \t" "\n" | tac | tr "\n" " "

If your tr doesn't grok \t and \n you will have to use octal codes or literal tabs and newlines.
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0