2

I want to program a script that allows what is said in the title. So basically I gunzip the initrd, than unpack the cpio, open vi to allow editing, save, pack with cpio, and gzip again, so nothing fancy here (at least I hope that, I am not good at shell scripting). Now after gunzipping the archive the ending .gzip or .gz is left out so that I can't use $1 as the name. How should I delete the ending so that I can use a new variable foo, for further processing?

This is probably not an very elegant way, but I hope it works :)

#/bin/bash
# This script should make it possible to edit the preseed file
# within a initrd gzipped cpio archive, without unpacking and packing it
# manually


mkdir temporarydirectory
# $1 will be the initrd (cpio archive which is compressed with gzip)
mv $1 temporarydirectory
cd temporarydirectory
gunzip $1
cpio -id < $1 # here is where i need to cut of the gzip ending
rm $1 # again without the gzip ending cutted of
vim preseed.cfg
find . | cpio -H newc -o > $1 # again without gzip ending
gzip $1 # here the same
mv $1 .. # here the gzip ending is used again
cd ..
rm -r temporarydirectory
Mat
  • 51,578
  • 10
  • 158
  • 140
Rob
  • 21
  • 1
  • Are you looking for [parameter expansion](http://unix.stackexchange.com/questions/22387/how-do-0-and-0-work/22390#22390)? – jasonwryan Jan 06 '12 at 06:43
  • You should test the initrd using file to find the compression method. I use xz to compress initrd and gunzip wouldn't work generically. – bsd Jan 06 '12 at 11:11
  • Not sure why you need to extract files in initrd, most (all) distros include a variant of mkinitrd. You could just edit the file as is, and then remake the initrd – bsd Jan 06 '12 at 14:33
  • Why not just pipe? `gunzip | cpio` and later `cpio | gzip`. Saves you all the trouble with unnecessary temporary files. – frostschutz Jul 12 '13 at 20:27

2 Answers2

0

I would change the next lines

gunzip $1
cpio -id < $1 

for

gzip -dc $1|cpio -id

and

mv $1 ..

for

mv ${1}.gz ../$1

YoMismo
  • 4,005
  • 1
  • 15
  • 31
0

Look at the bash Parameter Expansion docs. Removing an extension is pretty common, you can do it with:

...
file=$1
cpiofile=${file%.*}
...
gunzip $file
cpio -id < $cpiofile
...

(Replacing the positional parameters with proper variable names will make your script easier to read and maintain, especially if at some point you want to add or change the order of the parameters.)

Mat
  • 51,578
  • 10
  • 158
  • 140