LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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: 154

Rep: Reputation: 19
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: Yorkshire
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 627

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: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try this:
Code:
select FILENAME in ' NEITHER ' /usr/src/temp/*;
do
    if [ "$REPLY" != ' NEITHER ' ]; then
    ...
    fi
done
 
Old 07-16-2018, 11:23 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

 
Old 07-17-2018, 09:38 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
1 members found this post helpful.
  


Reply



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 04:32 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