Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
    
Are you looking for parameter expansion? –  jasonwryan Jan 6 '12 at 6: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. –  bdowning Jan 6 '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 –  bdowning Jan 6 '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
add comment

1 Answer

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.)

share|improve this answer
    
Or with basename. basename foo.gz .gz returns foo –  bdowning Jan 6 '12 at 14:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.