LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script to list tar files in directory and then extract based on user's input (https://www.linuxquestions.org/questions/programming-9/bash-script-to-list-tar-files-in-directory-and-then-extract-based-on-users-input-4175477876/)

socalheel 09-20-2013 09:12 AM

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


Firerat 09-20-2013 09:52 AM

Code:

#!/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

socalheel 09-20-2013 10:03 AM

Quote:

Originally Posted by Firerat (Post 5031412)
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

that's hot man. i was reading up on PS3 at (http://www.thegeekstuff.com/2008/09/...rompt_command/) and was trying to figure out how to add Quit to my list.

thank you so much.

Firerat 09-20-2013 10:41 AM

just for fun, switched it around a little

Code:

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

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

socalheel 09-20-2013 11:48 AM

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.

Firerat 09-20-2013 11:58 AM

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

socalheel 09-20-2013 01:07 PM

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

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


Firerat 09-20-2013 01:35 PM

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 )

socalheel 09-20-2013 01:40 PM

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.

Firerat 09-20-2013 01:44 PM

so you have it working now ?

socalheel 09-20-2013 02:01 PM

i do, what i posted works and verified several times.

Firerat 09-20-2013 02:21 PM

:) I missed the edit

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
  • confirm newarray with user
  • tar x

getting it 'fool proof' is the challenge

just have a stab when at a loose end..

socalheel 09-23-2013 09:24 AM

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.