Bash script - to save and run a command at the end
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.
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...
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.
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.
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.
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.
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.
#!/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
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.
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.
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
......
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.