LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2005, 08:20 AM   #1
bglnelissen
Member
 
Registered: Feb 2004
Location: Utrecht Holland
Distribution: Fedora, Debian, Mandrake, OSX, OpenBSD
Posts: 76

Rep: Reputation: 15
Lightbulb BASH: let output like 00004 and not 4


Well, i downloaded largefile.tar.bz2 but is was corrupted. So i did a bzip2recover and now i have allot of small .tar files. way too many to handle at ones. (when trying to "tar xvf *" it says: too many arguments (translated)).
So i wanted to write a little program that handles the files one by one. But i ran into a little problem.

The files are like:
rec00001file.tar
rec00002file.tar
rec00003file.tar
etc etc etc

i thought of a while loop and add 1 on the file name every loop.
like:
tar xvf rec00001file.tar;
tar xvf rec00002file.tar;
tar xvf rec00003file.tar;

but here is my problem. the let statement has such an output:

00001 becomes 1
00493 becomes 493
etc

(TEST IT YOURSELVES IN A CONSOLE)
Code:
let x=00000004;echo $x
but i want 00024 to stay 00024, how can i change this? is there an extra command for the statement let to do this?

thnx,
bas nelissen

(i read something about $i, but dont understand it)




Code:
#!/bin/bash
#tar-multi-unpacker
#made by b.nelissen

#QUESTIONS:
echo "hello world"
echo "starting you unpacking"
echo "whats the variable number of your first unpack item?:"
echo "     (like: 0001   is in this file:   rec0001file.tar)"
read number
echo "I am using:  $number"
echo ""

echo "whats the variable number of your last unpack item?:"
echo "     (like: 0845   is in this file:   rec0845file.tar)"
read numberhigh
echo "I am using:  $numberhigh"
echo ""

echo "whats the filename called BEFORE the variable number?:"
echo "     (like: rec    is in this file:   rec0845file.tar"
read befornumber
echo "I am using:  $befornumber"
echo ""

echo "whats the filename called AFTER the variable number?:"
echo "     (like: file.bz2    is in this file:   rec0845file.tar"
echo "     (its possible and mightbe easier to use a wildcard like: * )"
read afternumber
echo "I am using:  $afternumber"
echo ""

echo "where do you want me to unpack it to?"
echo "     (like: /home/bla/bla)"
echo "     (keep it empty for unpacking in the same dir)"
read outputdir

echo ""
echo "this is gonna be my command:"
echo " tar xvf $befornumber$number$afternumber $outputdir"
echo "to cancel press:  Ctrl-c"
sleep 10

#EXAMPLE
#while [ $number -le $numberhigh ];
#do echo $number;
#let number=$number+1;
#echo "";
#done


while [ $number -le $numberhigh ];
	echo $number;
	do tar xvf $befornumber$number$afternumber $outputdir;
        let number=$number+1;
        echo "";
        done

Last edited by bglnelissen; 09-27-2005 at 05:08 PM.
 
Old 09-27-2005, 11:54 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

The 0's (zero's) will be stripped, but you can pad variables.

Maybe you can use parts of this example (only echo's to terminal, nothing is actually done. based very loosly on the bottom part of your example).

Code:
#!/bin/bash

number=0
numberhigh=5
befornumber="before"
afternumber="after"
outputdir="/tmp/"

while (( $number <= $numberhigh ))
do
  paddednumber="`printf "%06d" $number`"
  echo "tar xvf ${befornumber}${paddednumber}${afternumber}.tar ${outputdir}"
  let number=number+1
  echo ""
done
This paddednumber="`printf "%06d" $number`" being the important part. The 06 part tells printf to add 0 (zero's) up to and including 6 chars.

For details on formatted printing: man 3 printf

Hope this gets you going again.
 
Old 09-27-2005, 11:59 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 855

Rep: Reputation: 203Reputation: 203Reputation: 203
for i in (rec*tar)
do
echo $i
done


Something like that???
 
Old 09-27-2005, 12:05 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Might I suggest:
Code:
find . -maxdepth 1 -iname "rec*file.tar" -exec tar xvf {} \;
 
Old 09-27-2005, 04:11 PM   #5
wmakowski
Member
 
Registered: Oct 2003
Location: Ohio
Distribution: Fedora 24, 25, RHL 5.2
Posts: 560

Rep: Reputation: 56
Similar to druuna's, but using a for loop.
Code:
#!/bin/bash
#
# Increment number to become part of file name.
#
                                                                                
for (( i=1; i<100; i++ ));do
  filenum=`printf "%05d" $i`
  fname=rec${filenum}file.tar
  echo $fname
done
Bill
 
Old 09-27-2005, 05:07 PM   #6
bglnelissen
Member
 
Registered: Feb 2004
Location: Utrecht Holland
Distribution: Fedora, Debian, Mandrake, OSX, OpenBSD
Posts: 76

Original Poster
Rep: Reputation: 15
Lightbulb thats the way to do it.

thnx for clearing this out.

a little test run with 13 as lucky numer:
Code:
$  paddednumber="`printf "%06d" 13`";echo $paddednumber

000013
So it works like a charm.
 
Old 09-27-2005, 06:02 PM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Re: thats the way to do it.

Quote:
Originally posted by bglnelissen
thnx for clearing this out.

a little test run with 13 as lucky numer:
Code:
$  paddednumber="`printf "%06d" 13`";echo $paddednumber

000013
So it works like a charm.
I'd recommend using $(..) instead of backticks - much easier to nest and looks neater, too:
Code:
$  paddednumber="$(printf "%06d" 13)";echo $paddednumber
 
Old 09-28-2005, 09:36 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
It's dead easy this one


Code:
$ typeset -Z4 x
$ x=2
$ echo $x
0002
$ x=123
$ echo $x
0123
$ x=9  
$ echo $x
0009
 
Old 09-28-2005, 10:16 AM   #9
wmakowski
Member
 
Registered: Oct 2003
Location: Ohio
Distribution: Fedora 24, 25, RHL 5.2
Posts: 560

Rep: Reputation: 56
typeset -Z works well in ksh, but not so good in bash.

$ typeset -Z5 x=2
bash: typeset: -Z: invalid option
typeset: usage: typeset [-afFirtx] [-p] name[=value] ...

Last edited by wmakowski; 09-28-2005 at 10:18 AM.
 
Old 09-28-2005, 10:24 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
doh

I forgot that one!


well, IMHO another example of how bash is far inferior for scripting.


bash a bit better interactively though I guess.
 
  


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
bash: put output from `ls` into an array samel_tvom Programming 18 07-31-2005 01:55 AM
BASH: Output everything between two strings systemparadox Programming 2 12-18-2004 11:26 AM
Read the output from a pipe with bash ? fluppi Linux - Software 3 01-13-2004 01:59 PM
Bash: How to concatenate the output of different commands? J_Szucs Programming 1 11-13-2003 08:43 AM
Command output conrolling in bash pazvant Linux - Software 1 10-24-2003 08:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:17 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