LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-29-2011, 09:50 AM   #16
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84

Quote:
Originally Posted by grail View Post
Code:
#!/bin/bash

(( ${#1} < 1 )) && exit || echo ${1:1:1}${1:0:1}$($0 ${1:2})
Amazing. I have one more squeezing round based on that:
Code:
#!/bin/bash
(( $1 )) && echo ${1:1:1}${1:0:1}$($0 ${1:2})
Ahahahahahah, man bash IS fun.

Actually we hit a pattern here, look how to use it to simulate "rev":
Code:
#!/bin/bash
last=$(( ${#1} - 1 ))
(( $1 )) && echo ${1:$last}$($0 ${1:0:$last})

Good thread btw. As to the issue with homework, hadn't actually given thought. Will try to have it into account in the future, though I wonder about "abandoned" threads when the OP disappears and a concrete answer didn't make it to any post...anyway I'm new here.

Last edited by Juako; 10-29-2011 at 04:01 PM.
 
Old 10-29-2011, 11:53 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Actually I prefer the previous as it will reverse any characters and not just numbers ... but the new revision is cool too
 
Old 10-29-2011, 12:19 PM   #18
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
To make it work with any character instead of numbers replace (( )) with [[ ]]

Code:
#!/bin/bash
[[ "$1" ]] && echo ${1:1:1}${1:0:1}$($0 ${1:2})
the "rev" example:
Code:
#!/bin/bash
last=$(( ${#1} - 1 ))
[[ "$1" ]] && echo ${1:$last}$($0 ${1:0:$last})
combining both examples to make a variable-length inverter:
Code:
#!/bin/bash
rev() {
    last=$(( ${#1} - 1 ))
    [[ "$1" ]] && echo "${1:$last}$($FUNCNAME "${1:0:$last}")"
}

variable_rev() {
    [[ $2 ]] && echo "$(rev ${2:0:$1})$($FUNCNAME $1 "${2:$1}")"
}

while read line; do
    $variable_rev $1 "$line"
done

Last edited by Juako; 10-31-2011 at 05:32 PM. Reason: added example
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Reverse Tunneling / Reverse port forwarding in SSH dynamics Linux - Networking 5 02-07-2017 07:19 AM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
[SOLVED] bind reverse zone; no name with reverse lookup deadeyes Linux - Server 3 10-12-2009 09:54 AM
SOLVED: Trying to "reverse engineer" a calculation: any number hackers out there? drkstr Slackware 22 08-01-2006 01:01 AM


All times are GMT -5. The time now is 12:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration