LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 11-01-2004, 07:45 AM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Rep: Reputation: 52
Bash script - to save and run a command at the end


Hi folks,

I have a bash script with following line running at its end
.........
......
# Remove ISO File...
rm -i $ISO_File
- End of Script -

The script is working without problem.

Now I expect to set this line as argument, i.e. removing it leaving only there;
........
.......
# Remove ISO File ...
(Empty)
- End of Script -

Somewhere at the beginning of the Script add following lines
.......
read -p "Remove ISO File <yes|no>:"
if [ "$REPLY" = "yes" ]; then
echo "rm $ISO_File"
read
etc.

I have no idea how to get the line "rm $ISO_File" saved after
# Remove ISO File...

and run at the end of the script

Please advise. TIA

B.R.
satimis
 
Old 11-01-2004, 07:49 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,378

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
i don't understand your problem at all.. it sounds like you're saying you don't know how to use your text editor?? if that's the case and vi or emacs are confusing the whatever's out of you, just use a different editor, like nano or pico.
 
Old 11-01-2004, 08:38 AM   #3
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Original Poster
Rep: Reputation: 52
Hi acid_kewpie,

Quote:
i don't understand your problem at all.. it sounds like you're saying you don't know how to use your text editor?? if that's the case and vi or emacs are confusing the whatever's out of you, just use a different editor, like nano or pico.
Sorry, nothing with the editing device.

What command/syntax shall I add after;

read -p "Remove ISO File <yes|no>:"
if [ "$REPLY" = "yes" ]; then
?????

1)
if [ "$REPLY" = "yes" ]; then
echo "rm $ISO_File" >> cdmaker
(cdmaker=name of this script)

OR

2)
if [ "$REPLY" = "yes" ]; then
echo "rm $ISO_File"
(anything else)

so that this command (removing ISO image/File) will be executed at the end after CD burnt.

TIA

B.R.
satimis
 
Old 11-01-2004, 10:02 AM   #4
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Do you mean that a running scripts hould modify its own code, then run further as modified?

I think this does not make much sense, and is also impossible: the script code is read in when you start the script, and any subsequent modifications to its code will only take effect when you run the script the next time.

Why do not use a conditional expression at the and of the code, instead?:

Code:
removeiso="no" 
echo "Remove ISO file at the end (Y/N)?" 
read removeiso 
case $removeiso in 
[Yy]*) removeiso="yes" ;; 
*) removeiso="no" ;; 
esac
.
.
.
if [[ $removeiso == "yes" ]] ; then
  rm $ISO_File 
fi
Damn, I cannot correctly reproduce the if syntax here, as the forum program always changes it... Anyway, you can see the idea, supposed that this is what you want.

Last edited by J_Szucs; 11-01-2004 at 10:10 AM.
 
Old 11-01-2004, 10:35 AM   #5
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Original Poster
Rep: Reputation: 52
Hi J_Szucs,

Tks for your advice.

Quote:
Do you mean that a running scripts hould modify its own code, then run further as modified?

I think this does not make much sense, and is also impossible: the script code is read in when you start the script, and any subsequent modifications to its code will only take effect when you run the script the next time.
I fully agree with your comment. It was only a suggestion indicating adding the command at end of the script. Besides it will be added to the script permanently.

Quote:
Why do not use a conditional expression at the and of the code, instead?:
......
The existing "-i" option at the finally command/line already works for me. It will popup a confirmation "deleting ISO File" for me to say <yes/no>
Code:
.........
......
# Remove ISO File...
rm -i $ISO_File
- End of Script -
What I need is to have all arguments selected at start NOT after burning the CD and prompting another selection.

B.R.
satimis
 
Old 11-01-2004, 11:24 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
You could just hold the answer until the end of script :
Code:
# begin of script
read -p "Remove ISO File <yes|no>:"
ANSWER=$REPLY

# your script here...
...
if [ "$ANSWER" = "yes" ]; then
    rm ...
fi
# end of script

Last edited by Cedrik; 11-01-2004 at 11:26 AM.
 
Old 11-01-2004, 02:18 PM   #7
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 245

Rep: Reputation: 42
Meta reply - Don't use bash.

Bash is nice as a command line.

Bash is nice to string a couple of commands together.

Bash is a bloody awful language to try program in.

Seriously, as a programming language it just plain stinks.

Suggestion, learn one of the decent scripting languages, perl, python or ruby I care not which.

Personally, I would recommend ruby.

http://www.ruby-lang.org
 
Old 11-01-2004, 03:26 PM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Bash is used here for what it is good for, satimis wrote a little script to automatize ISO image creation.
 
Old 11-01-2004, 11:14 PM   #9
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Original Poster
Rep: Reputation: 52
Hi Cedrik,

Tks for your advice.

Quote:
You could just hold the answer until the end of script :
That is what I need, input answer at start and holding it until the end then executing the command.

Test performed as follow;
Script:-
Code:
#!/bin/bash
set -x

#Set directory path...
user=$(whoami)
now=$(date +%Y.%m.%d.%R)
ISO_File="/tmp/satimis/image_${user}_${now}.iso"

# Remove ISO File...
# begin of script
read -p "Remove ISO File <yes|no>:"
ANSWER=$REPLY
...
# My script
...
# Remove ISO File...
if [ "$ANSWER" = "yes" ]; then
    rm $ISO_File
fi
Print out:-
Code:
......
+ ISO_File=/tmp/satimis/image_satimis_2004.11.02.12:48.iso
+ read -p 'Remove ISO File <yes|no>:'
Remove ISO File <yes|no>:yes
+ ANSWER=yes
......
+ rm -i /tmp/satimis/image_satimis_2004.11.02.12:48.iso
rm: remove regular file `/tmp/satimis/image_satimis_2004.11.02.12:48.iso'? y
+ '[' yes = yes ']'
+ rm /tmp/satimis/image_satimis_2004.11.02.12:48.iso
rm: cannot remove `/tmp/satimis/image_satimis_2004.11.02.12:48.iso': No such file or directory
ISO file removed but need 2nd confirmation which I try to avoid.

Any suggestion. TIA

B.R.
satimis
 
Old 11-01-2004, 11:26 PM   #10
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Original Poster
Rep: Reputation: 52
Hi J_Szucs,

Your script tested as follow;

Script:-
Code:
#!/bin/bash
set -x

#Set directory path...
user=$(whoami)
now=$(date +%Y.%m.%d.%R)
ISO_File="/tmp/satimis/image_${user}_${now}.iso"

# Remove ISO File...
removeiso="no"
echo "Remove ISO file at the end (Y|N)"
read removeiso
case $removeiso in
[Yy]*) removeiso="yes" ;;
*) removeiso="no" ;;
esac
.....
# My script
.....
# Remove ISO File...
if [ "$removeiso" = "yes" ];then
rm $ISO_File
fi
Printout:-
+ '[' no = yes ']'

Sorry, can't remove ISO File

B.R.
satimis
 
Old 11-01-2004, 11:37 PM   #11
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Original Poster
Rep: Reputation: 52
Re: Meta reply - Don't use bash.

Hi cyent,
Quote:
Bash is nice as a command line.

Bash is nice to string a couple of commands together.

Bash is a bloody awful language to try program in.

Seriously, as a programming language it just plain stinks.

Suggestion, learn one of the decent scripting languages, perl, python or ruby I care not which.

Personally, I would recommend ruby.

http://www.ruby-lang.org
Tks for your comment and URL

I started learning programming about 10 days and will move to Perl+qk or Python soon.

When I first time set my feet on Linux World I always looked for GUI commands because I came from M$Windows. On discussion forums folks said to me "here is not M$Window" As time going on, later I became loving command lines rather than GUI. They allow me learning the basic instead of GUI front end.

B.R.
satimis
 
Old 11-02-2004, 12:26 AM   #12
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Original Poster
Rep: Reputation: 52
Solution found

Hi Cedrik,

Further to my previous posting, your advice/additional script works seamlessly.

Previously I forgot to comment out the line "rm -i $ISO_File" in my original script.
Code:
....
# Remove ISO File..
rm -i $ISO_File
....
....
# Remove ISO File..
if [ "$ANSWER" = "yes" ]; then
    rm $ISO_File
fi
therefore my script asked me to confirm again.

Sorry for that and tks.

B.R.
satimis
 
Old 11-02-2004, 03:42 AM   #13
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
I do not know why my script did not work for you, as it does exacly the same as Cedric's script: holds the answer (storing it in a variable) until the end of the script, and removes the file the same way.

The only difference between Cedric's and my script is the it reads in the choice in a different way: cedrics script requires that the answer should be strictly a lowercase 'yes' for removing the file, while my code allows a single 'Y' (either lowercase or uppercase) in the choice, too.

Maybe the failure is due to a syntax error in the if...then...fi part of the script?

I told you I cannot post here the exact syntax I use, as the linuxquestions forum program changes my code, even if I enclose it between the 'code' tags.
E.g. the double '[' and ']' and double '=' characters around (and in) the if condition are totally misinterpreted and removed by the forum software.
Besides, I always put a space before the ";" and the "then" keywords, which were not present in your copy of my code.

Last edited by J_Szucs; 11-02-2004 at 03:47 AM.
 
Old 11-02-2004, 05:39 AM   #14
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,385

Original Poster
Rep: Reputation: 52
Hi J_Szucs [/i]

Quote:
I do not know why my script did not work for you, ......
Thx for your further advice.

Re-edited the script as advised and ran it afterwards.

Script:-
Code:
.....
# Remove ISO File
removeiso="no"
echo "Remove ISO file at the end (Y|N)"
read removeiso
case $removeiso in
[Yy]*) removeiso="yes" ;;
*) removeiso="no" ;;
esac
....
# My script
....
# Remove ISO File...
if [ "$removeiso" = "yes" ] ; then
   rm $ISO_File
fi
Printout
Code:
.....
+ echo 'Remove ISO file at the end (Y|N)'
Remove ISO file at the end (Y|N)
+ read removeiso
y
+ removeiso=yes
.......
.......
+ '[' yes = yes ']'
+ rm /tmp/satimis/image_satimis_2004.11.02.19:09.iso
ISO file removed at the end. Your script also works without problem. Thx.

Regarding your code/syntax
Code:
case $removeiso in
[Yy]*) removeiso="yes" ;;
*) removeiso="no" ;;
esac
making accepting either lower/upper case input.

My code/syntax
Code:
.....
REPLY="B"
until [ -z "$REPLY" ]

do
echo "Choose one of the following:"
echo
echo "[D]ocument"
echo "[P]hoto"
echo "[B]oth"
echo
echo "[Enter] = Exit"
echo

read

case "$REPLY" in

"F" | "f" )
# Accept upper or lowercase input.
Document
;;

"P" | "p" )
Photo
;;

"B" | "b" )
Docuement
Photo
;;

* )
#Default option.
# Do nothing for other keys
;;

esac
done
......
Is there any way to shorten them?

TIA

B.R.
satimis
 
Old 11-02-2004, 07:11 AM   #15
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
This will be possibly shorter, thought this will run once (not in a loop like yours):

choices="[D]ocument [P]hoto [B]oth [Enter]=Exit"
echo "Choose one of the following:"
for choice in $choices ; do echo $choice ; done

read reply

case $reply in
[Dd]*) Document ;;
[Pp]*) Photo ;;
[Bb]*) Document ; Photo ;;
*) echo "Exiting..." ; exit ;;
esac

Last edited by J_Szucs; 11-02-2004 at 07:15 AM.
 
  


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
run-parts gives error on bash script ChoKamir Programming 6 01-31-2012 01:36 AM
Run my bash script as a daemon. jaimese Linux - Newbie 12 02-10-2011 03:28 PM
how to run one bash script from within another? babag Programming 9 04-28-2005 12:12 AM
Run script at login instead of bash uzi4u Linux - General 2 04-28-2004 02:31 PM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM


All times are GMT -5. The time now is 10:55 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration