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.
trying to learn a bit of bash scripting and could do with some help.
I have a script (below) that lists the contents of a folder and allows you to choose a number to delete the item. What I would like to do is display as line 1 the option to exit incase would don't want to make a choice.
I am not sure how to added this and what command I would need and where so any help here would be great, thanks
#!/bin/bash
echo "This script can make can delete any file or folder displayed below"
echo "Enter the number of the folder you wish to delete:"
select FILENAME in /usr/src/temp/*;
do
echo "You picked $FILENAME ($REPLY), This folder is now deleted"
rm -rf "$FILENAME"
done
I'd recommend that you learn about bash scripting some other way than deleting files.
Because if you make a mistake, you can make the mistake of deleting the incorrect files. And since you only have a notification not a confirmation step, then you'll now the bad thing happened, but be powerless to say "N" to your script.
"set -xv" is very powerful for debugging. It enables debugging and verbose output to the command prompt when you run your script.
I have a bash blog link in my signature. There are a number of recommendations in there for writing and debugging scripts.
I'd also recommend you use proper grammar in the outputs of your scripts.
I think there is a bug in post#3; must be either
[ "$FILENAME" != " NEITHER " ] or
[ "$REPLY" != 1 ]
The following works only with $FILENAME by means of a case-esac
Code:
select FILENAME in EXIT /usr/src/temp/*
do
case $FILENAME in
(EXIT) break
;;
("") echo "illegal choice"
;;
(*)
echo "You picked $FILENAME ($REPLY), This folder is now deleted"
rm -rf "<$FILENAME>"
esac
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.