Register
It is currently Sun Apr 20, 2014 1:14 pm

rar script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Aug 29, 2011 9:33 am   

Joined: Mon Aug 29, 2011 9:22 am
Posts: 3
Hello folks,
so this has been driving me crazy for a while. most torrents i download these days are rar'ed. about half the people use the part[##].rar format, the other half use the .r[##]. i download lots of stuff all at once, and then giving the rar e command to each job is tedious, especially when youre looking at a boxset or something with lots of subdirectories all with their own rar sets. so i wrote this script, but it doesnt work. the problem creeps up when either the source or dest has a space in the path. i guess i need to replaces spaces with "\ " but i havent figured out how to do that successfully. please help!

thanks!


this finds the first rar file in each subdirectory and explodes it to your destination directory

usage: UNRAR <<directoryContainingRarsOrSubdirectorysWithRars>> <<destination>>

Code:
#!/bin/bash
# UNRAR
# This is a little script for recursively finding rar files and exploding them into the dest dir

E_BADARGS=65
if [ ! -n "$2" ]
then
        echo "Usage: UNRAR <<DirToRecursivelyExplode>> <<Destination>>"
        exit $E_BADARGS
fi

for f in `ls -d $1*/ 2> /dev/null`
do
        UNRAR $f $2
done
rars=`ls $1*rar 2> /dev/null |head -1`

if [ -n ${rars[0]} ]
then
        rar e ${rars[0]} $2
fi

exit 0


it must only unrar the first rar file of each directory, so in the cases where the rar is broken into volumes named like:

file.part01.rar
file.part02.rar
file.part03.rar
file.part04.rar
etc

it cannot unrar each of those or it would be unraring the same contents several times--this precludes the very elegant

Code:
#!/bin/bash
d="$(realpath "$2")"
find "$1" -type f -name '*rar' -print0 | while read -d $'\0' f; do
    (cd "$(dirname "$f")" && rar e "$(basename "$f")" $d
done


:(

here is some sample folder structure if it helps



Code:


ls -R
.:
LARGEFILES dest dir

./LARGEFILES:
LARGEFILE.p01      LARGEFILE.p11
LARGEFILE.p02           LARGEFILE.p12
LARGEFILE.p03           LARGEFILE.p13
LARGEFILE.p04           LARGEFILE.p14
LARGEFILE.p05           LARGEFILE.p15
LARGEFILE.p06        LARGEFILE.p16
LARGEFILE.p07           LARGEFILE.p17
LARGEFILE.p08           LARGEFILE.p18
LARGEFILE.p09           LARGEFILE.p19
LARGEFILE.p10           LARGEFILE.p20

./LARGEFILES/LARGEFILE.p01:
LARGEFILE101.nfo         LARGEFILE101.part13.rar  LARGEFILE101.part26.rar  LARGEFILE101.part39.rar
LARGEFILE101.part01.rar  LARGEFILE101.part14.rar  LARGEFILE101.part27.rar  LARGEFILE101.part40.rar
LARGEFILE101.part02.rar  LARGEFILE101.part15.rar  LARGEFILE101.part28.rar  LARGEFILE101.part41.rar
LARGEFILE101.part03.rar  LARGEFILE101.part16.rar  LARGEFILE101.part29.rar  LARGEFILE101.part42.rar
LARGEFILE101.part04.rar  LARGEFILE101.part17.rar  LARGEFILE101.part30.rar  LARGEFILE101.part43.rar
LARGEFILE101.part05.rar  LARGEFILE101.part18.rar  LARGEFILE101.part31.rar  LARGEFILE101.part44.rar
LARGEFILE101.part06.rar  LARGEFILE101.part19.rar  LARGEFILE101.part32.rar  LARGEFILE101.part45.rar
LARGEFILE101.part07.rar  LARGEFILE101.part20.rar  LARGEFILE101.part33.rar  LARGEFILE101.part46.rar
LARGEFILE101.part08.rar  LARGEFILE101.part21.rar  LARGEFILE101.part34.rar  LARGEFILE101.part47.rar
LARGEFILE101.part09.rar  LARGEFILE101.part22.rar  LARGEFILE101.part35.rar  LARGEFILE101.part48.rar
LARGEFILE101.part10.rar  LARGEFILE101.part23.rar  LARGEFILE101.part36.rar  LARGEFILE101.part49.rar
LARGEFILE101.part11.rar  LARGEFILE101.part24.rar  LARGEFILE101.part37.rar  LARGEFILE101.part50.rar
LARGEFILE101.part12.rar  LARGEFILE101.part25.rar  LARGEFILE101.part38.rar  LARGEFILE101.sfv

./LARGEFILES/LARGEFILE.p02:
LARGEFILE.102.nfo  LARGEFILE.102.r06  LARGEFILE.102.r13  LARGEFILE.102.r20
LARGEFILE.102.r00  LARGEFILE.102.r07  LARGEFILE.102.r14  LARGEFILE.102.r21
LARGEFILE.102.r01  LARGEFILE.102.r08  LARGEFILE.102.r15  LARGEFILE.102.r22
LARGEFILE.102.r02  LARGEFILE.102.r09  LARGEFILE.102.r16  LARGEFILE.102.r23
LARGEFILE.102.r03  LARGEFILE.102.r10  LARGEFILE.102.r17  LARGEFILE.102.rar
LARGEFILE.102.r04  LARGEFILE.102.r11  LARGEFILE.102.r18  LARGEFILE.102.sfv
LARGEFILE.102.r05  LARGEFILE.102.r12  LARGEFILE.102.r19

./LARGEFILES/LARGEFILE.p03:
LARGEFILE.p03.nfo  LARGEFILE.p03.r13
LARGEFILE.p03.r00  LARGEFILE.p03.r14
LARGEFILE.p03.r01  LARGEFILE.p03.r15
LARGEFILE.p03.r02  LARGEFILE.p03.r16
LARGEFILE.p03.r03  LARGEFILE.p03.r17
LARGEFILE.p03.r04  LARGEFILE.p03.r18
LARGEFILE.p03.r05  LARGEFILE.p03.r19
LARGEFILE.p03.r06  LARGEFILE.p03.r20
LARGEFILE.p03.r07  LARGEFILE.p03.r21
LARGEFILE.p03.r08  LARGEFILE.p03.r22
LARGEFILE.p03.r09  LARGEFILE.p03.r23
LARGEFILE.p03.r10  LARGEFILE.p03.rar
LARGEFILE.p03.r11  LARGEFILE.p03.sfv
LARGEFILE.p03.r12

./LARGEFILES/LARGEFILE.p04:
f104.nfo         f104.part07.rar  f104.part14.rar  f104.part21.rar
f104.part01.rar  f104.part08.rar  f104.part15.rar  f104.part22.rar
f104.part02.rar  f104.part09.rar  f104.part16.rar  f104.part23.rar
f104.part03.rar  f104.part10.rar  f104.part17.rar  f104.part24.rar
f104.part04.rar  f104.part11.rar  f104.part18.rar  f104.part25.rar
f104.part05.rar  f104.part12.rar  f104.part19.rar  f104.sfv
f104.part06.rar  f104.part13.rar  f104.part20.rar

./LARGEFILES/LARGEFILE.p05:
LARGEFILE105.nfo         LARGEFILE105.part07.rar  LARGEFILE105.part14.rar  LARGEFILE105.part21.rar
LARGEFILE105.part01.rar  LARGEFILE105.part08.rar  LARGEFILE105.part15.rar  LARGEFILE105.part22.rar
LARGEFILE105.part02.rar  LARGEFILE105.part09.rar  LARGEFILE105.part16.rar  LARGEFILE105.part23.rar
LARGEFILE105.part03.rar  LARGEFILE105.part10.rar  LARGEFILE105.part17.rar  LARGEFILE105.part24.rar
LARGEFILE105.part04.rar  LARGEFILE105.part11.rar  LARGEFILE105.part18.rar  LARGEFILE105.part25.rar
LARGEFILE105.part05.rar  LARGEFILE105.part12.rar  LARGEFILE105.part19.rar  LARGEFILE105.sfv
LARGEFILE105.part06.rar  LARGEFILE105.part13.rar  LARGEFILE105.part20.rar

./LARGEFILES/LARGEFILE.p06:
LARGEFILE.p06.nfo  LARGEFILE.p06.r06  LARGEFILE.p06.r13
LARGEFILE.p06.r00  LARGEFILE.p06.r07  LARGEFILE.p06.r14
LARGEFILE.p06.r01  LARGEFILE.p06.r08  LARGEFILE.p06.r15
LARGEFILE.p06.r02  LARGEFILE.p06.r09  LARGEFILE.p06.r16
LARGEFILE.p06.r03  LARGEFILE.p06.r10  LARGEFILE.p06.r17
LARGEFILE.p06.r04  LARGEFILE.p06.r11  LARGEFILE.p06.rar
LARGEFILE.p06.r05  LARGEFILE.p06.r12  LARGEFILE.p06.sfv

./LARGEFILES/LARGEFILE.p07:
f107.nfo         f107.part14.rar
f107.part01.rar  f107.part15.rar
f107.part02.rar  f107.part16.rar
f107.part03.rar  f107.part17.rar
f107.part04.rar  f107.part18.rar
f107.part05.rar  f107.part19.rar
f107.part06.rar  f107.part20.rar
f107.part07.rar  f107.part21.rar
f107.part08.rar  f107.part22.rar
f107.part09.rar  f107.part23.rar
f107.part10.rar  f107.part24.rar
f107.part11.rar  f107.part25.rar
f107.part12.rar  f107.sfv
f107.part13.rar

./LARGEFILES/LARGEFILE.p08:
LARGEFILE.p08. .nfo  LARGEFILE.p08. .r08  LARGEFILE.p08. .r17
LARGEFILE.p08. .r00  LARGEFILE.p08. .r09  LARGEFILE.p08. .r18
LARGEFILE.p08. .r01  LARGEFILE.p08. .r10  LARGEFILE.p08. .r19
LARGEFILE.p08. .r02  LARGEFILE.p08. .r11  LARGEFILE.p08. .r20
LARGEFILE.p08. .r03  LARGEFILE.p08. .r12  LARGEFILE.p08. .r21
LARGEFILE.p08. .r04  LARGEFILE.p08. .r13  LARGEFILE.p08. .r22
LARGEFILE.p08. .r05  LARGEFILE.p08. .r14  LARGEFILE.p08. .r23
LARGEFILE.p08. .r06  LARGEFILE.p08. .r15  LARGEFILE.p08. .rar
LARGEFILE.p08. .r07  LARGEFILE.p08. .r16  LARGEFILE.p08. .sfv

./LARGEFILE/LARGEFILE.p09:
f109.nfo         f109.part09.rar  f109.part18.rar
f109.part01.rar  f109.part10.rar  f109.part19.rar
f109.part02.rar  f109.part11.rar  f109.part20.rar
f109.part03.rar  f109.part12.rar  f109.part21.rar
f109.part04.rar  f109.part13.rar  f109.part22.rar
f109.part05.rar  f109.part14.rar  f109.part23.rar
f109.part06.rar  f109.part15.rar  f109.part24.rar
f109.part07.rar  f109.part16.rar  f109.part25.rar
f109.part08.rar  f109.part17.rar  f109.sfv

./LARGEFILES/LARGEFILE.p10:
safe110.nfo         safe110.part07.rar  safe110.part14.rar  safe110.part21.rar
safe110.part01.rar  safe110.part08.rar  safe110.part15.rar  safe110.part22.rar
safe110.part02.rar  safe110.part09.rar  safe110.part16.rar  safe110.part23.rar
safe110.part03.rar  safe110.part10.rar  safe110.part17.rar  safe110.part24.rar
safe110.part04.rar  safe110.part11.rar  safe110.part18.rar  safe110.part25.rar
safe110.part05.rar  safe110.part12.rar  safe110.part19.rar  safe110.sfv
safe110.part06.rar  safe110.part13.rar  safe110.part20.rar

./LARGEFILES/LARGEFILE.p11:
LARGEFILE.111.bound.nfo         LARGEFILE.111.bound.part14.rar
LARGEFILE.111.bound.part01.rar  LARGEFILE.111.bound.part15.rar
LARGEFILE.111.bound.part02.rar  LARGEFILE.111.bound.part16.rar
LARGEFILE.111.bound.part03.rar  LARGEFILE.111.bound.part17.rar
LARGEFILE.111.bound.part04.rar  LARGEFILE.111.bound.part18.rar
LARGEFILE.111.bound.part05.rar  LARGEFILE.111.bound.part19.rar
LARGEFILE.111.bound.part06.rar  LARGEFILE.111.bound.part20.rar
LARGEFILE.111.bound.part07.rar  LARGEFILE.111.bound.part21.rar
LARGEFILE.111.bound.part08.rar  LARGEFILE.111.bound.part22.rar
LARGEFILE.111.bound.part09.rar  LARGEFILE.111.bound.part23.rar
LARGEFILE.111.bound.part10.rar  LARGEFILE.111.bound.part24.rar
LARGEFILE.111.bound.part11.rar  LARGEFILE.111.bound.part25.rar
LARGEFILE.111.bound.part12.rar  LARGEFILE.111.bound.sfv
LARGEFILE.111.bound.part13.rar

./LARGEFILES/LARGEFILE.p12:
LARGEFILE.112.nfo         LARGEFILE.112.part14.rar
LARGEFILE.112.part01.rar  LARGEFILE.112.part15.rar
LARGEFILE.112.part02.rar  LARGEFILE.112.part16.rar
LARGEFILE.112.part03.rar  LARGEFILE.112.part17.rar
LARGEFILE.112.part04.rar  LARGEFILE.112.part18.rar
LARGEFILE.112.part05.rar  LARGEFILE.112.part19.rar
LARGEFILE.112.part06.rar  LARGEFILE.112.part20.rar
LARGEFILE.112.part07.rar  LARGEFILE.112.part21.rar
LARGEFILE.112.part08.rar  LARGEFILE.112.part22.rar
LARGEFILE.112.part09.rar  LARGEFILE.112.part23.rar
LARGEFILE.112.part10.rar  LARGEFILE.112.part24.rar
LARGEFILE.112.part11.rar  LARGEFILE.112.part25.rar
LARGEFILE.112.part12.rar  LARGEFILE.112.sfv
LARGEFILE.112.part13.rar

./LARGEFILES/LARGEFILE.p13:
LARGEFILE.113.nfo         LARGEFILE.113.part14.rar
LARGEFILE.113.part01.rar  LARGEFILE.113.part15.rar
LARGEFILE.113.part02.rar  LARGEFILE.113.part16.rar
LARGEFILE.113.part03.rar  LARGEFILE.113.part17.rar
LARGEFILE.113.part04.rar  LARGEFILE.113.part18.rar
LARGEFILE.113.part05.rar  LARGEFILE.113.part19.rar
LARGEFILE.113.part06.rar  LARGEFILE.113.part20.rar
LARGEFILE.113.part07.rar  LARGEFILE.113.part21.rar
LARGEFILE.113.part08.rar  LARGEFILE.113.part22.rar
LARGEFILE.113.part09.rar  LARGEFILE.113.part23.rar
LARGEFILE.113.part10.rar  LARGEFILE.113.part24.rar
LARGEFILE.113.part11.rar  LARGEFILE.113.part25.rar
LARGEFILE.113.part12.rar  LARGEFILE.113.sfv
LARGEFILE.113.part13.rar

./LARGEFILES/LARGEFILE.p14:
LARGEFILE.114.nfo         LARGEFILE.114.part14.rar
LARGEFILE.114.part01.rar  LARGEFILE.114.part15.rar
LARGEFILE.114.part02.rar  LARGEFILE.114.part16.rar
LARGEFILE.114.part03.rar  LARGEFILE.114.part17.rar
LARGEFILE.114.part04.rar  LARGEFILE.114.part18.rar
LARGEFILE.114.part05.rar  LARGEFILE.114.part19.rar
LARGEFILE.114.part06.rar  LARGEFILE.114.part20.rar
LARGEFILE.114.part07.rar  LARGEFILE.114.part21.rar
LARGEFILE.114.part08.rar  LARGEFILE.114.part22.rar
LARGEFILE.114.part09.rar  LARGEFILE.114.part23.rar
LARGEFILE.114.part10.rar  LARGEFILE.114.part24.rar
LARGEFILE.114.part11.rar  LARGEFILE.114.part25.rar
LARGEFILE.114.part12.rar  LARGEFILE.114.sfv
LARGEFILE.114.part13.rar

./LARGEFILES/LARGEFILE.p15:
LARGEFILE.115.nfo         LARGEFILE.115.part14.rar
LARGEFILE.115.part01.rar  LARGEFILE.115.part15.rar
LARGEFILE.115.part02.rar  LARGEFILE.115.part16.rar
LARGEFILE.115.part03.rar  LARGEFILE.115.part17.rar
LARGEFILE.115.part04.rar  LARGEFILE.115.part18.rar
LARGEFILE.115.part05.rar  LARGEFILE.115.part19.rar
LARGEFILE.115.part06.rar  LARGEFILE.115.part20.rar
LARGEFILE.115.part07.rar  LARGEFILE.115.part21.rar
LARGEFILE.115.part08.rar  LARGEFILE.115.part22.rar
LARGEFILE.115.part09.rar  LARGEFILE.115.part23.rar
LARGEFILE.115.part10.rar  LARGEFILE.115.part24.rar
LARGEFILE.115.part11.rar  LARGEFILE.115.part25.rar
LARGEFILE.115.part12.rar  LARGEFILE.115.sfv
LARGEFILE.115.part13.rar

./LARGEFILES/LARGEFILE.p16:
LARGEFILE.116..nfo         LARGEFILE.116..part14.rar
LARGEFILE.116..part01.rar  LARGEFILE.116..part15.rar
LARGEFILE.116..part02.rar  LARGEFILE.116..part16.rar
LARGEFILE.116..part03.rar  LARGEFILE.116..part17.rar
LARGEFILE.116..part04.rar  LARGEFILE.116..part18.rar
LARGEFILE.116..part05.rar  LARGEFILE.116..part19.rar
LARGEFILE.116..part06.rar  LARGEFILE.116..part20.rar
LARGEFILE.116..part07.rar  LARGEFILE.116..part21.rar
LARGEFILE.116..part08.rar  LARGEFILE.116..part22.rar
LARGEFILE.116..part09.rar  LARGEFILE.116..part23.rar
LARGEFILE.116..part10.rar  LARGEFILE.116..part24.rar
LARGEFILE.116..part11.rar  LARGEFILE.116..part25.rar
LARGEFILE.116..part12.rar  LARGEFILE.116..sfv
LARGEFILE.116..part13.rar

./LARGEFILES/LARGEFILE.p17:
LARGEFILE.p17.nfo            xor-LARGEFILE.117.r06  xor-LARGEFILE.117.r13  xor-LARGEFILE.117.r20
xor-LARGEFILE.117.r00               xor-LARGEFILE.117.r07  xor-LARGEFILE.117.r14  xor-LARGEFILE.117.r21
xor-LARGEFILE.117.r01               xor-LARGEFILE.117.r08  xor-LARGEFILE.117.r15  xor-LARGEFILE.117.r22
xor-LARGEFILE.117.r02               xor-LARGEFILE.117.r09  xor-LARGEFILE.117.r16  xor-LARGEFILE.117.r23
xor-LARGEFILE.117.r03               xor-LARGEFILE.117.r10  xor-LARGEFILE.117.r17  xor-LARGEFILE.117.rar
xor-LARGEFILE.117.r04               xor-LARGEFILE.117.r11  xor-LARGEFILE.117.r18  xor-LARGEFILE.117.sfv
xor-LARGEFILE.117.r05               xor-LARGEFILE.117.r12  xor-LARGEFILE.117.r19

./LARGEFILES/LARGEFILE.p18:
LARGEFILE.118..nfo  LARGEFILE.118..r10  LARGEFILE.118..r21  LARGEFILE.118..r32
LARGEFILE.118..r00  LARGEFILE.118..r11  LARGEFILE.118..r22  LARGEFILE.118..r33
LARGEFILE.118..r01  LARGEFILE.118..r12  LARGEFILE.118..r23  LARGEFILE.118..r34
LARGEFILE.118..r02  LARGEFILE.118..r13  LARGEFILE.118..r24  LARGEFILE.118..r35
LARGEFILE.118..r03  LARGEFILE.118..r14  LARGEFILE.118..r25  LARGEFILE.118..r36
LARGEFILE.118..r04  LARGEFILE.118..r15  LARGEFILE.118..r26  LARGEFILE.118..r37
LARGEFILE.118..r05  LARGEFILE.118..r16  LARGEFILE.118..r27  LARGEFILE.118..rar
LARGEFILE.118..r06  LARGEFILE.118..r17  LARGEFILE.118..r28  LARGEFILE.118..sfv
LARGEFILE.118..r07  LARGEFILE.118..r18  LARGEFILE.118..r29
LARGEFILE.118..r08  LARGEFILE.118..r19  LARGEFILE.118..r30
LARGEFILE.118..r09  LARGEFILE.118..r20  LARGEFILE.118..r31

./LARGEFILES/LARGEFILE.p19:
LARGEFILE.p19.nfo  LARGEFILE.p19.r06  LARGEFILE.p19.r13
LARGEFILE.p19.r00  LARGEFILE.p19.r07  LARGEFILE.p19.r14
LARGEFILE.p19.r01  LARGEFILE.p19.r08  LARGEFILE.p19.r15
LARGEFILE.p19.r02  LARGEFILE.p19.r09  LARGEFILE.p19.r16
LARGEFILE.p19.r03  LARGEFILE.p19.r10  LARGEFILE.p19.r17
LARGEFILE.p19.r04  LARGEFILE.p19.r11  LARGEFILE.p19.rar
LARGEFILE.p19.r05  LARGEFILE.p19.r12  LARGEFILE.p19.sfv

./LARGEFILE/LARGEFILE.p20:
LARGEFILE.120..nfo  LARGEFILE.120..r06  LARGEFILE.120..r13  LARGEFILE.120..r20
LARGEFILE.120..r00  LARGEFILE.120..r07  LARGEFILE.120..r14  LARGEFILE.120..r21
LARGEFILE.120..r01  LARGEFILE.120..r08  LARGEFILE.120..r15  LARGEFILE.120..r22
LARGEFILE.120..r02  LARGEFILE.120..r09  LARGEFILE.120..r16  LARGEFILE.120..r23
LARGEFILE.120..r03  LARGEFILE.120..r10  LARGEFILE.120..r17  LARGEFILE.120..rar
LARGEFILE.120..r04  LARGEFILE.120..r11  LARGEFILE.120..r18  LARGEFILE.120..sfv
LARGEFILE.120..r05  LARGEFILE.120..r12  LARGEFILE.120..r19

./dest dir:







thanks!


Top
 Profile  
 PostPosted: Mon Aug 29, 2011 10:28 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hello,

without reading too much, I'd say
Use More Quotes
don't use `ls` (it's usually a bad thing in scripting)
Code:
for f in "$1"/*; do ...; done


Code:
if [ ! -n "$arg" ]
=
Code:
if [ -z "$2" ]


rars as you wrote it is not an array, ${rar[0]} is first element of an array, an array with only one element is of no use.


Top
 Profile  
 PostPosted: Mon Aug 29, 2011 3:23 pm   

Joined: Mon Aug 29, 2011 9:22 am
Posts: 3
thanks for your help!
so

Quote:
Code:
if [ ! -n "$arg" ]
=
Code:
if [ -z "$2" ]


i did this because i wanted to make sure there were 2 arguments, is that not necessary you think?

Quote:
rars as you wrote it is not an array, ${rar[0]} is first element of an array, an array with only one element is of no use.


well, except that it is an array... i only want One and exactly One file named *.rar from each folder, and i only want to explode the first one; because, as i mentioned above, sometimes the files are named like
bigfile.part01.rar
bigfile.part02.rar
bigfile.part03.rar
bigfile.part04.rar
bigfile.part05.rar
and i only want to explode one of them. so i took the output of ls filtered for *rar into an array and took just the first element. theres probably a better way to do this....

ill try to figure out the quotes thing, thanks for the advice


Top
 Profile  
 PostPosted: Mon Aug 29, 2011 3:55 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
to make sure there were 2 arguments :
Code:
if (( $# == 2 )); then...


"the quote thing" is :
Code:
$ anyFunction () { echo $1;}
$ var="foo bar baz"
$ anyFunction $var
foo
$ anyFunction "$var"
foo bar baz

again, don't use ls for the reason above (also because if "eval is evil", then "ls is hells". XD).
you'd better use bash's globbing capabilities.

an array is assigned like this:
Code:
array=( item 'item 2' etc )


Top
 Profile  
 PostPosted: Tue Aug 30, 2011 7:29 am   

Joined: Mon Aug 29, 2011 9:22 am
Posts: 3
ah, i think were getting close, what about if
var="foo bar baz"

and i want the output to be
foo\ bar\ baz

use sed?


Top
 Profile  
 PostPosted: Tue Aug 30, 2011 10:59 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
Quote:
i want the output to be
foo\ bar\ baz
use non-POSIX bash's parameter expansion // :
Code:
var="foo bar baz"
echo "${var// /\ }"


Top
 Profile  
 PostPosted: Tue Sep 20, 2011 12:28 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
nosenseofhumor1 wrote:
ah, i think were getting close, what about if
var="foo bar baz"

and i want the output to be
foo\ bar\ baz

use sed?


You can use %q printf(0)'s format. ( which isn't POSIX either, but it's fine since you're using bash(1) )

Code:
var='foo bar baz'
printf '%q\n' "$var"


→ foo\ bar\ baz


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP