bash script to list tar files in directory and then extract based on user's input
i am coming up with a script to list the tar.gz files in my directory in a menu format and asking the user to enter select which file they would like to extract. i have that part working but i would like to add the option that if the entry is invalid, to start over. as it stands now, if an invalid entry is entered, the script quits.
based on what i've read, select is a good method for this, so i went with select. if there is a better option, then feel free to list them. here is what i have: Code:
#!/bin/bash |
Code:
#!/bin/bash The above case, is basically nested if statement we only break ( from select ) when we have Quit or a tar.gz selected I also removed the ( ls | grep | awk ) stuff Edit: added "" (red) which will help if you have spaces or the like in the filenames |
Quote:
thank you so much. |
just for fun, switched it around a little
Code:
Tarballs=(*.tar*) Tarballs is an array ${#Tarballs[@]} is the number of elements in the array $(( $x + 1 )) some simple maths All so you can have Quit at the end, and tell them what the number will be ;) http://mywiki.wooledge.org/BashGuide/Arrays not a double paste... http://mywiki.wooledge.org/BashSheet#Arrays arrays are good, as they will help you deal with things like spaces in filenames. Wish I had known about arrays much earlier :D |
i like the additions and that's exactly the direction i was currently working on (trying to get the Quit to the end of the list).
with this script, is it also possible to add the option to extract ALL the tar files? you don't have to do it for me, just tell me if it's possible and if i need help with it, i can PM you. |
Sure is possible
add another option, like the Quit and use the Tarballs array http://mywiki.wooledge.org/BashGuide/Arrays should help regards PM I prefer things like this 'out in the open' as it may help others |
got it. and i was only going to PM you i needed more guidance in hopes you would have posted in here. i didn't want to make this thread too long. :)
removed it since the ALL statement works with echo but won't with tar -xvf ... i will post up my solution when i get it. now it's fixed :D Code:
#!/bin/bash |
The only threads that are too long are the ones which boarder on flamewar ;)
I have a feeling I know what the problem is, I'll let you work on it For a bit ;) actually, just noticed my array version has a couple of little 'bugs', $file should be ${file} and both the ${file} should be double quoted "${file}" will only present a problem if special char. in filename ( like space ) |
thanks man for all the help on this. one more little step towards me understanding scripting :)
the link to http://mywiki.wooledge.org/BashGuide/Arrays helped me figure out i wasn't selecting each element in the array properly. |
so you have it working now ?
|
i do, what i posted works and verified several times.
|
:) I missed the edit
yeah, basically the same as mine, I was a little lazy with the for i Code:
Tarballs=(*.tar*) next trick.. select multiple, but not all ;) can be done with select, but might be faster ( for user ) to use
getting it 'fool proof' is the challenge just have a stab when at a loose end.. |
multiple file selection? oooh, good exercise. let me work on that today since it seems like a slow day (or at least starting out as such).
|
All times are GMT -5. The time now is 10:35 PM. |