LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-22-2008, 09:22 AM   #1
bfellenr
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Rep: Reputation: 0
Question Bash script help... menu to pick file from folder


Hello,

I'm trying to figure out how to make a bash script that will basically give you a menu of files in a folder then let you pick one. I would like to have it desplay a number, then the tgz filename, then read text from a file (only contains a single line) with the same name but ending in txt. There would be no more than 10 files. Example folder and screen mockup below.

Folder structure...
-------------------
file-2008-02-20.tgz
file-2008-02-20.txt
file-2008-02-21.tgz
file-2008-02-21.txt
file-2008-02-22.tgz
file-2008-02-22.txt


Menu mock up...
---------------

Extraction process
==================

1. file-2008-02-20.tgz : Here is the single line read from file-2008-02-20.txt

2. file-2008-02-21.tgz : Here is the single line read from file-2008-02-21.txt

3. file-2008-02-22.tgz : Here is the single line read from file-2008-02-22.txt

Pick the file to extract:




Thanks for the help
 
Old 02-22-2008, 09:41 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
select file in *; do tar xf $file; done

-- maybe not - I'm not sure I follow what you want to do with the text files but this should at least point you in the right direction.

Last edited by slakmagik; 02-22-2008 at 09:42 AM. Reason: read problem description more carefully
 
1 members found this post helpful.
Old 02-22-2008, 01:52 PM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Code:
l_count=0
ls *.tgz|while read l_file
do
   l_count=`expr $l_count + 1`
   echo "${l_count}. ${l_file} `cat $(l_file%.tgz).txt`"
done
echo
echo "Pick the file to extract:"
Not tested as I am waiting to go home...
 
Old 02-24-2008, 10:09 AM   #4
bfellenr
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
That worked for the most part. The problem I have that when I try to set the filename to the FILE# variable in the do loop it doesn't stay set when the loop is exited even though I don't declare it as local. I need to basically equate the number they type to the filename so I can perform my operation after their selection.

Code:
L_COUNT=0
FILE0=0 FILE2=0 FILE3=0 FILE4=0 FILE5=0 FILE6=0 FILE07= FILE8=0 FILE9=0

ls *.tgz|while read L_FILE

do
	echo $L_COUNT. $L_FILE : `cat ${L_FILE%.tgz}.txt`

	case "$L_COUNT" in
		0) FILE0=$L_FILE ;;
		1) FILE1=$L_FILE ;;
		2) FILE2=$L_FILE ;;
		3) FILE3=$L_FILE ;;
		4) FILE4=$L_FILE ;;
		5) FILE5=$L_FILE ;;
		6) FILE6=$L_FILE ;;
		7) FILE7=$L_FILE ;;
		8) FILE8=$L_FILE ;;
		9) FILE9=$L_FILE ;;
	esac

	L_COUNT=`expr $L_COUNT + 1`
done

echo
echo -n "Pick the file to extract: "
read -e CHOICE
echo

case "$CHOICE" in
	0) CHOICE=$FILE0 ;;
	1) CHOICE=$FILE1 ;;
	2) CHOICE=$FILE2 ;;
	3) CHOICE=$FILE3 ;;
	4) CHOICE=$FILE4 ;;
	5) CHOICE=$FILE5 ;;
	6) CHOICE=$FILE6 ;;
	7) CHOICE=$FILE7 ;;
	8) CHOICE=$FILE8 ;;
	9) CHOICE=$FILE9 ;;
esac

echo You picked $CHOICE
 
Old 02-25-2008, 02:50 AM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
export the variables:
Code:
case "$L_COUNT" in
		0) export FILE0=$L_FILE ;;
 
Old 02-26-2008, 06:48 AM   #6
bfellenr
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Tried that. While in the do loop it works just fine. I can get the value that was set in the case with/without doing the export. The minute the script exits the for loop the value is back to what it was first initialized as.
 
Old 02-26-2008, 08:00 AM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Code:
#!/bin/sh
tarballs=(*.tar.gz)
txtfiles=(*.txt)
for (( i=0; i<${#tarballs[*]}; i++ )); do
    echo $i: ${tarballs[$i]}: $(cat ${txtfiles[$i]})
done
read -ep "Which one? "
tar xf ${tarballs[$REPLY]}
The above untars the file I pick by number. A "loop runs in a subshell when it's part of a pipeline." FAQ #24

-- Oh - it might be considered more user-friendly to start the list from 1.

Code:
#!/bin/sh
tarballs=(*.tar.gz)
txtfiles=(*.txt)
for (( i=0; i<${#tarballs[*]}; i++ )); do
    echo $(($i+1)): ${tarballs[$i]}: $(cat ${txtfiles[$i]})
done
read -ep "Which one? "
tar xf ${tarballs[$(($REPLY-1))]}

Last edited by slakmagik; 02-26-2008 at 08:04 AM. Reason: User friendliness
 
Old 01-13-2011, 03:21 PM   #8
520Soul
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Rep: Reputation: 0
Question

hm...let me try the last script to see if it work....thanks guy...

---------- Post added 01-13-11 at 04:21 PM ----------

Quote:
Originally Posted by slakmagik View Post
Code:
#!/bin/sh
tarballs=(*.tar.gz)
txtfiles=(*.txt)
for (( i=0; i<${#tarballs[*]}; i++ )); do
    echo $i: ${tarballs[$i]}: $(cat ${txtfiles[$i]})
done
read -ep "Which one? "
tar xf ${tarballs[$REPLY]}
The above untars the file I pick by number. A "loop runs in a subshell when it's part of a pipeline." FAQ #24

-- Oh - it might be considered more user-friendly to start the list from 1.

Code:
#!/bin/sh
tarballs=(*.tar.gz)
txtfiles=(*.txt)
for (( i=0; i<${#tarballs[*]}; i++ )); do
    echo $(($i+1)): ${tarballs[$i]}: $(cat ${txtfiles[$i]})
done
read -ep "Which one? "
tar xf ${tarballs[$(($REPLY-1))]}




######### i still got problem using the script that you post. #############

Code:
#!/bin/sh
tarballs=(*.tar.gz)
txtfiles=(*.txt)
for (( i=0; i<${#tarballs[*]}; i++ )); do
    echo $(($i+1)): ${tarballs[$i]}: $(cat ${txtfiles[$i]})
done
read -ep "Which one? "
tar xf ${tarballs[$(($REPLY-1))]}
[/QUOTE]




################## sorry i'm a newbie on bash...please help..below is the problem that i've been having. #####################


echo This is test
1: *.tar.gz:
Which one? zzz
./test.sh: line 60: tarballs: bad array subscript <----------
tar: Option f requires an argument <---------- this two line show the problem. it need " f " for an argument? please help ..
Usage:
List: tar -tf <archive-filename>
Extract: tar -xf <archive-filename>
Create: tar -cf <archive-filename> [filenames...]
Help: tar --help
 
Old 01-14-2011, 03:57 PM   #9
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
I can't quite follow your post. If you're trying the little snippet and getting error messages it's because it's basically a demo that does no error checking and expects the tarballs and text files to be in the current directory and expects a valid argument. If you don't have such files, you'll get '1: *.tar.gz' when glob expansion fails and the array won't be populated and tar will have nothing to work on.
 
Old 01-15-2011, 03:44 AM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Additionally, if you are on a system where /bin/sh is not a symbolic link to /bin/bash you would get the following error:
Quote:
./test3.sh: 2: Syntax error: "(" unexpected
Therefore you should replace
Code:
#!/bin/sh
with
Code:
#!/bin/bash
As per slakmagik, this code was specific for the Original Poster's task with no real error checking in place.

If you are running this exact code then your error message should have said:
Quote:
1: *.tar.gz: This is a test
Which one? q
./test.sh: line 8: tarballs: bad array subscript
tar: Old option `f' requires an argument.
Try `tar --help' or `tar --usage' for more information.
Your error occurs because the response was not numeric, but you would still have had issues with a numeric value outside the range for the array.
 
Old 01-15-2011, 09:43 AM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Version with basic testing added:
Code:
#!/bin/bash
tarballs=(*.tar.gz)
txtfiles=(*.txt)
for (( i=0; i<${#tarballs[*]}; i++ )); do
    if [ $i -eq 0 ]
    then
       if [ "${tarballs[$i]}" == '*.tar.gz' ]
       then
          echo "no tar files found!"
          exit 1
       fi
    fi
    if [ "${txtfiles[$i]}" == '*.txt' -o "${txtfiles[$i]}" == '' -o $i -gt ${#txtfiles[*]}]
    then
       echo $(($i+1)): ${tarballs[$i]}
    else
       echo $(($i+1)): ${tarballs[$i]}: $(cat ${txtfiles[$i]})
    fi
done
read -ep "Which one? "
if [ "${REPLY}A" == "$(echo $REPLY|sed 's/[^0-9]//g')A" -a "$REPLY" != '']
then
   ## Numeric
   if [ $REPLY -le ${#tarballs[*]} ]
   then
      echo "Extracting ${tarballs[$((REPLY-1))]}"
      tar xf ${tarballs[$(($REPLY-1))]}
   else
      echo "numeric value out of range"
      exit 2
   fi
else
   echo "$REPLY not numeric"
   exit 3
fi

Last edited by Disillusionist; 01-15-2011 at 09:49 AM. Reason: Tidying up and adding exit codes
 
1 members found this post helpful.
Old 01-15-2011, 10:23 AM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by bfellenr View Post
Hello,

I'm trying to figure out how to make a bash script that will basically give you a menu of files in a folder then let you pick one. I would like to have it desplay a number, then the tgz filename, then read text from a file (only contains a single line) with the same name but ending in txt. There would be no more than 10 files. Example folder and screen mockup below.

Folder structure...
-------------------
file-2008-02-20.tgz
file-2008-02-20.txt
file-2008-02-21.tgz
file-2008-02-21.txt
file-2008-02-22.tgz
file-2008-02-22.txt


Menu mock up...
---------------

Extraction process
==================

1. file-2008-02-20.tgz : Here is the single line read from file-2008-02-20.txt

2. file-2008-02-21.tgz : Here is the single line read from file-2008-02-21.txt

3. file-2008-02-22.tgz : Here is the single line read from file-2008-02-22.txt

Pick the file to extract:




Thanks for the help
heres my stab at it (season to taste):
Code:
[liveuser@localhost temp]$ cat bfellenr.ksh 
#!/bin/bash

echo select a file:
num=1
for item in `ls -1 *.mt`
do
 txt=`echo $item | sed s/mt/txt/`
 echo $num: $item: `cat $txt` 
 num=`expr $num + 1`
done | tee menu.lst

read num
#echo num = $num
sed -n "$num"p menu.lst | cut -d : -f 2
[liveuser@localhost temp]$ ./bfellenr.ksh 
select a file:
1: akuma.mt: gouki
2: chun-li.mt: shadow-law
3: h4x0rz.mt: hacker
4: hello.mt: hi
5: l33t.mt: elite
6: world.mt: planet
5
 l33t.mt
 
Old 01-15-2011, 01:56 PM   #13
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by Disillusionist View Post
Additionally, if you are on a system where /bin/sh is not a symbolic link to /bin/bash you would get the following error:


Therefore you should replace
Code:
#!/bin/sh
with
Code:
#!/bin/bash
Good point. The OP was asking for bash and that's what it is, but I must have typed sh out of habit.
 
Old 01-15-2011, 03:30 PM   #14
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by schneidz View Post
heres my stab at it (season to taste):
Code:
[liveuser@localhost temp]$ cat bfellenr.ksh 
#!/bin/bash

echo select a file:
num=1
for item in `ls -1 *.mt`
do
 txt=`echo $item | sed s/mt/txt/`
 echo $num: $item: `cat $txt` 
 num=`expr $num + 1`
done | tee menu.lst

read num
#echo num = $num
sed -n "$num"p menu.lst | cut -d : -f 2
[liveuser@localhost temp]$ ./bfellenr.ksh 
select a file:
1: akuma.mt: gouki
2: chun-li.mt: shadow-law
3: h4x0rz.mt: hacker
4: hello.mt: hi
5: l33t.mt: elite
6: world.mt: planet
5
 l33t.mt
To resolve any issues with missing txt files you could modify it slightly:

Code:
 echo $num: $item: `cat $txt 2>/dev/null`
 
1 members found this post helpful.
  


Reply

Tags
bash, script



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
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
bash script extract name transfer & to right folder gizmo1007 Programming 2 12-15-2007 05:12 AM
Urgent : creating folder using bash script prernasin Linux - Newbie 2 09-26-2007 01:58 AM
bash script, for all files in folder do ............ Simon_6162 Programming 2 07-18-2007 02:40 PM
howto pick bash variables from file imagineers7 Linux - Newbie 3 05-08-2006 01:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:45 AM.

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
Open Source Consulting | Domain Registration