Linux - NewbieThis 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
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.
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.
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.
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.
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
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
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.
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.
#!/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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.