[SOLVED] bash script to list tar files in directory and then extract based on user's input
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
cd /<location_of_tar_files>
clear
PS3="Enter the number of the backup file to restore from or 'q' to quit: "
list=$(ls -la | grep "tar.gz" | awk '{print $9}')
select file in $list; do
if [ -n "$file" ]; then
tar -xvf ${file}
fi
break
done
#!/bin/bash
cd /<location_of_tar_files>
clear
PS3="Enter the number of the backup file to restore from or '1' to quit: "
select file in Quit *.tar.gz; do
case $file in
'') echo >&2 "Please select a numbered option";;
Quit) break;;
?*) echo tar -xvf "${file}";break;;
esac
done
The problem was, your break was always executed
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
The problem was, your break was always executed
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
Tarballs=(*.tar*)
PS3="Enter the number of the backup file to restore from or '$((${#Tarballs[@]}+1))' to quit: "
select file in "${Tarballs[@]}" Quit; do
case $file in
'') echo >&2 "Please select a numbered option";;
Quit) break;;
?*) echo tar -xvf ${file};break;;
esac
done
a few things going on
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
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.
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
Code:
#!/bin/bash
cd /<location_of_tar_files>
tarfiles=(*.tar*)
clear
PS3="Enter the number of the backup file to restore from or '$((${#tarfiles[@]}+1))' for ALL or '$((${#tarfiles[@]}+2))' to Quit: "
select file in "${tarfiles[@]}" ALL Quit; do
case $file in
'') echo "Please select a numbered option";;
Quit) break;;
ALL) for tarfile in "${tarfiles[@]}"; do tar -xvf "$tarfile";done;break;;
?*) tar -xvf ${file};break;;
esac
done
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 )
yeah, basically the same as mine, I was a little lazy with the for i
Code:
Tarballs=(*.tar*)
PS3="Enter the number of the backup file to restore from or '$((${#Tarballs[@]}+2))' to quit: "
select file in "${Tarballs[@]}" "All tarballs" Quit; do
case "${file}" in
'') echo >&2 "Please select a numbered option";;
Quit) break;;
All*) for i in "${Tarballs[@]}";do
tar -xvf "${i}"
done;break;;
?*) tar -xvf "${file}";break;;
esac
done
notice my PS3 is different
next trick..
select multiple, but not all
can be done with select, but might be faster ( for user ) to use
array
printf array # for loop, numbered
read
add each 'element' of the read's string into newarray # depending on number scheme 'correct' to start at 0
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.