LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2013, 09:12 AM   #1
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Rep: Reputation: 3
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
 
Old 09-20-2013, 09:52 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,495

Rep: Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689
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

Last edited by Firerat; 09-20-2013 at 09:55 AM.
 
Old 09-20-2013, 10:03 AM   #3
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by Firerat View Post
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.
 
Old 09-20-2013, 10:41 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,495

Rep: Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689
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
 
Old 09-20-2013, 11:48 AM   #5
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Original Poster
Rep: Reputation: 3
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.
 
Old 09-20-2013, 11:58 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,495

Rep: Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689
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
 
Old 09-20-2013, 01:07 PM   #7
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Original Poster
Rep: Reputation: 3
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

Last edited by socalheel; 09-20-2013 at 01:33 PM.
 
Old 09-20-2013, 01:35 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,495

Rep: Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689
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 )
 
Old 09-20-2013, 01:40 PM   #9
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Original Poster
Rep: Reputation: 3
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.
 
Old 09-20-2013, 01:44 PM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,495

Rep: Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689
so you have it working now ?
 
Old 09-20-2013, 02:01 PM   #11
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Original Poster
Rep: Reputation: 3
i do, what i posted works and verified several times.
 
Old 09-20-2013, 02:21 PM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,495

Rep: Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689Reputation: 689
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..
 
Old 09-23-2013, 09:24 AM   #13
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Original Poster
Rep: Reputation: 3
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
List Files in parent directory for user input lucie2012 Programming 5 10-06-2012 04:34 AM
[SOLVED] Re-naming and using user input files in bash script sready Programming 3 02-05-2012 11:37 PM
[SOLVED] Bash Script; Sort files into directory based on data in the file name MTAS Programming 31 10-06-2010 11:47 AM
Bash script rename files based on directory name cupofnestor Linux - General 7 03-31-2010 08:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration