PDA

View Full Version : Bash script-confirmation option



satimis
19th October 2004, 07:46 PM
Hi folks,

I have following script to create ISO image

#!/bin/bash
# Set ISO filename...
.....
dir1="/home/satimis/Document/"
dir2="/home/satimis/Photo/"

.....
# Create ISO file...
mkisofs -R -o "$ISO_File" -l -graft-points -hide-rr-moved \
/Document/=$dir1 \
/Photo/=$dir2 \
$list
.....How to set confirmation on
/Document/=$dir1
/Photo/=$dir2

before the script will continue to proceed,i.e. to have "accept" or "ignore" options on them

Kindly advise. TIA

B.R.
satimis

pigpen
21st October 2004, 05:11 PM
Sorry, but I don't fully understand your question.
What is it exacty that you want to confirm?
Do you want to confirm if the directories actually exist?
Or do you need some kind of user input in your script?
If your problem has to do with the latter then I'm pretty sure
you will find the answer in the Advanced Bash-Scripting Guide (http://www.tldp.org/LDP/abs/html/)

satimis
22nd October 2004, 03:42 AM
Hi pigpen,

Tks for your response and URL

I re-organize my question as follows in anticipation to make it clearer;

I have following command line embedded on a desktop icon and a script. They work for me.

Command line


xterm -hold -T "CDMaker by satimis" -e "su -c /path/to/cdmake"

Script
#!/bin/bash
# Set ISO filename...
user=$(whoami)
now=$(date +%Y.%m.%d.%R)
ISO_File="/home/satimis/To_burn/image_${user}_${now}.iso"
dir1="/home/satimis/Document/"
dir2="/home/satimis/Photo/"

# Loop to get directories...
echo "Enter directories to burn. Leave blank to end list"
entry="empty"
list=""
count=0
until [ -z $entry ]; do
echo -n "Enter a directory: "
read entry
if [ $entry != "" ]; then
list="$list $1$entry"
fi
done

# Create ISO file...
mkisofs -R -o "$ISO_File" -l -graft-points -hide-rr-moved \
/Document/=$dir1 \
/Photo/=$dir2 \
$list

# Burn ISO file to disc
cdrecord dev=ATA:0,0,0 -v -eject “$ISO_File”

# Remove ISO file
rm “$ISO_File”
Steps taken as follow;
1)Clicking the icon starting xterm
2)Enter password to login as root
3)Enter further directories, if any, with corresponding path in the form of “/Dir_AAA/=/path/to/Dir_AAA, etc. (cdrecord format). (If no further entry just hit <Enter> to continue)

Remark:
dir1="/home/satimis/Document/"
dir2="/home/satimis/Photo/"
are permanently added.

Then the script will take care of the rest and a CD is burnt finally.

Now I need to re-edit;
1)The command line as


xterm -hold -T "CDMaker by satimis" -e /path/to/cdmake"

Let the script to handle login as root to execute commands on background.

2)Re-edit the script to perform

a)
Login as root after ISO_File created because USER is not allowed to burn CD.
(I don't want to add USER to the wheel using this route. Hoping to learn more on script)

b)
Re-edit the script to perform following steps.

b-1)
The script is evoked by the revised command line

b-2)
Before
dir1="/home/satimis/Document/"
dir2="/home/satimis/Photo/"

are added, confirmation is now required. It will pop up for entry of “yes” or “no”. If “yes” they will be added. If “no” they will not be added. The script will continue to next command asking for adding of further directory/directories with corresponding path.

Remark:
dir1="/home/satimis/Document/"
dir2="/home/satimis/Photo/"
are still added on the script

b-3)
After an ISO_file created, it will continue to login as root asking for input of root password.

b-4)
After a CD burnt, xterm will close. I'll remove the option “-hold” later to get it done.

Please advise. I'm now getting into some confusing.

TIA

B.R.
Satimis

pigpen
23rd October 2004, 12:17 PM
Here's what you might need:
[pigpen@mybox pigpen]$ cat dialog.sh
#!/bin/bash
dialog --inputbox "Shall we do it or not?" 10 60
if [ $? != 0 ] ; then
echo "Coward ;-)"
exit
else
echo "OK! Here we go..."
fiPops up a dialog box. The script exits if you hit "Cancel".

satimis
23rd October 2004, 05:07 PM
Hi pigpen,

Tks for your advice.

I have no idea where shall I put following 2 lines
/Document/=$dir1 \
/Photo/=$dir2

I tried several positions;

......
# Create ISO file...
mkisofs -R -o "$ISO_File" -l -graft-points -hide-rr-moved \
$list \

dialog --inputbox "Shall we do it or not?" 10 60
/Document/=$dir1 \
/Photo/=$dir2
if [ $? != 0 ]" ; then
echo "Coward ;-)"
exit
else
echo "OK! Here we go..."
fi
......so that they will appear inside the box one after another
(i.e.
/Document/=/home/satimis/Document/
hit <OK>
/Photo/=/home/satimis/Photo/
hit <OK>)

Then the script will continue to create ISO File
(Remark: I re-positioned the line "$list \")

Please advise. TIA

B.R.
satimis

pigpen
23rd October 2004, 10:06 PM
How about saving the exit codes of the 'dialog' program in two variables and evaluate them later?
I haven't tried, but
dialog --inputbox $dir1 10 60should work, too.
BTW: I still think, that http://www.tldp.org/LDP/abs/html/ would be a valuable resource for you.

satimis
24th October 2004, 02:20 AM
Hi pigpen,

Tks for your advice and URL. I tried but unfortunately it did not work.

"$dir" became the name of the box.

Following works to put "/Document/=/home/satimis/Document" inside the box


# Create ISO file...
mkisofs -R -o "$ISO_File" -l -graft-points -hide-rr-moved \
$list \

dialog --inputbox "Shall we do it or not?" 10 60
/Document/=$dir \
if [ $? != 0 ] ; then
echo "Coward ;-)"
exit
else
echo "OK! Here we go..."
fi
ISO File created finally but excluding the directory "/Document/=/home/satimis/Document"

I still can't find a solution.

B.R.
satimis

pigpen
24th October 2004, 04:57 PM
I don't want to take the fun out of your programming experience by writing the script for you.
Therefore I give you this link: http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
The only way to become a programmer is through experience. And it's fun, too, I promise ;)

satimis
24th October 2004, 05:03 PM
Hi pigpen,

Tks for your URL.

I tried several hours discovering follows finally;

1)

....
# Create ISO file...
mkisofs -R -o "$ISO_File" -l -graft-points -hide-rr-moved \
/Document/=$dir1 \
/Photo/=$dir2 \
$list
....
The above lines can't be separated otherwise "mkisofs" won't work.

2)
The possible solution is creating new directories "/home/satimis/Temp/dir1", "/home/satimis/Temp/dir2" and do the trick on the beginning of the script;


I made following test;

3A)
$ mkdir /home/satimis/Temp/dir1
$ mkdir /home/satimis/Temp/dir2

3B)
Edited the script as follow;
#!/bin/bash

# Set ISO filename...
user=$(whoami)

now=$(date +%Y.%m.%d.%R)
ISO_File="/home/satimis/To_burn/image_${user}_${now}.iso"
dir1=/home/satimis/Temp/dir1/
dir2=/home/satimis/Temp/dir2/

read -p "Add Document directory?"
if [ "$REPLY" = "yes" ]; then
cp -r /home/satimis/Document/* /home/satimis/Temp/dir1/
else
cancel
fi

read -p "Add Photo directory?"
if [ "$REPLY" = "yes" ]; then
cp -r /home/satimis/Photo/* /home/satimis/Temp/dir2/
else
cancel
fi

# Loop to get directories...
echo "Enter directories to burn. Leave blank to end list"
entry="empty"
list=""
until [ -z $entry ]; do
echo -n "Enter a directory: "
read entry
if [ $entry != "" ]; then
list="$list $1$entry"
fi
done

# Create ISO file...
mkisofs -R -o "$ISO_File" -l -graft-points -hide-rr-moved \
/Document/=$dir1 \
/Photo/=$dir2 \
$list

# Burn ISO file...
cdrecord dev=ATA:1,0,0 -v -eject "$ISO_File"

# Delete diretories and files...
rm -r /home/satimis/Temp/dir1/* /home/satimis/Temp/dir2/*

# Delete ISO file...
rm "$ISO_File"
Although I solve the probem now. But this is a stupid way which takes longer time to complete because of copying directories and files to "Temp directories" and deleting them thereafter.

Have you had any suggestion.

TIA

B.R.
satimis