Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-13-2018, 11:12 AM   #1
Registered: Dec 2009
Posts: 110

Rep: Reputation: 18
Bash script help

Hi All

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


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/*;
echo "You picked $FILENAME ($REPLY), This folder is now deleted"
rm -rf "$FILENAME"
Old 07-13-2018, 12:01 PM   #2
Registered: Aug 2008
Location: UK
Distribution: openSUSE(Leap and Tumbleweed) and a regularly changing third
Posts: 316

Rep: Reputation: Disabled
Look in your bash tutorial for "read" and see how you can fit it in your script. Come back to us if you have further issues.
Old 07-13-2018, 03:07 PM   #3
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,595

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
Try this:
select FILENAME in ' NEITHER ' /usr/src/temp/*;
    if [ "$REPLY" != ' NEITHER ' ]; then
Old 07-16-2018, 11:23 AM   #4
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,155
Blog Entries: 12

Rep: Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634
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.
Old 07-17-2018, 03:11 AM   #5
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,432

Rep: Reputation: 203Reputation: 203Reputation: 203
The user can press ctrl-C

Old 07-17-2018, 09:38 AM   #6
Registered: Dec 2011
Location: Simplicity
Posts: 899

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
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
select FILENAME in EXIT /usr/src/temp/*
  case $FILENAME in
  (EXIT) break
  ("") echo "illegal choice"
    echo "You picked $FILENAME ($REPLY), This folder is now deleted"
    rm -rf "<$FILENAME>"
1 members found this post helpful.


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
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

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