LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-13-2018, 11:12 AM   #1
glennbtn
Member
 
Registered: Dec 2009
Posts: 108

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

#!/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
 
Old 07-13-2018, 12:01 PM   #2
petelq
Member
 
Registered: Aug 2008
Location: UK
Distribution: openSUSE(Leap and Tumbleweed) and a regularly changing third
Posts: 293

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
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,503

Rep: Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080
Try this:
Code:
select FILENAME in ' NEITHER ' /usr/src/temp/*;
do
    if [ "$REPLY" != ' NEITHER ' ]; then
    ...
    fi
done
 
Old Today, 11:23 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 6,778
Blog Entries: 12

Rep: Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411Reputation: 2411
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.
 
  


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
[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

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

All times are GMT -5. The time now is 07:36 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