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 10-24-2004, 11:34 PM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Rep: Reputation: 56
Bash script - repeated command


Hi folks,

I have a script which needs following command to repeat confirmation on questions.

Code:
......
read -p "Add Document directory <yes/no>"
if [ "$REPLY" = "yes" ]; then
  cp -r /home/satimis/Document/* /home/satimis/Temp/dir1/
else
  cancel
fi

read -p "Add Photo directory <yes/no>"
if [ "$REPLY" = "yes" ]; then

  cp -r /home/satimis/Photo/* /home/satimis/Temp/dir2/
else
  cancel
fi
.....
If for 10+ confirmations there will be a long script. Kindly advise how to avoid it.

TIA

B.R.
satimis
 
Old 10-25-2004, 12:20 AM   #2
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101

Rep: Reputation: 15
y dont' you just use the while loop??
 
Old 10-25-2004, 01:26 AM   #3
nitin_batta
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15
Just make a seperate file having the questions and then use the script as

Code:
for i in `cat q_file`
do
read -p "$i <yes/no>"
if [ "$REPLY" = "yes" ]; then
  cp -r /home/satimis/Document/* /home/satimis/Temp/dir1/
else
  cancel
fi
q_file is the file containing all the question u ask i the script.
 
Old 10-25-2004, 02:02 AM   #4
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
But "cp -r /home/satimis/Document/*" does not suggest the folder name. Thus only copies the "Document/" contents only everytime. So it is not fuctional for what satimis is trying to do.

How about:

Code:
for i in `cat q_file`
do
read -p "Whould you like $i <yes/no>? "
        if [ "$REPLY" = "yes" ]; then
        cp -r /home/satimis/$i/* /home/satimis/Temp/dir1/
        fi
done
This assuming "q_file" looks like:
Photo
Documents
New_Folder
Whatever

Also I think you want:

cp -r /home/satimis/$i/ /home/satimis/Temp/dir1/

Because you want to copie the whole directory and not just the files within right?
 
Old 10-25-2004, 02:18 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
But micxz is missing the dir1, dir2, ... requirement, what about:
Code:
i=0
for dir in Document Photo etcetera
do
  read -p "Add $dir directory <yes/no> "
  if [ "$REPLY" = "yes" ]; then
    i=$((i+1))
    cp -r /home/satimis/$dir/* /home/satimis/Temp/dir$i/
  else
    break # not sure of that one, what was the initial "cancel" for ?
  fi
done

Last edited by jlliagre; 10-25-2004 at 02:20 AM.
 
Old 10-25-2004, 02:25 AM   #6
nitin_batta
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15
Quote:
This assuming "q_file" looks like:
Photo
Documents
New_Folder
Whatever

Also I think you want:

cp -r /home/satimis/$i/ /home/satimis/Temp/dir1/

Because you want to copie the whole directory and not just the files within right
Point taken i just missed on that.
 
Old 10-25-2004, 02:31 AM   #7
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally posted by jlliagre But micxz is missing the dir1, dir2, ... requirement
for i in `cat q_file`

is the same as

for i in Document Photo.....

In this example, so how am I missing dir1 and dir2...
 
Old 10-25-2004, 02:41 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
If you look closely to the requirements, each copy is going to a different directory:
Document is going to dir1
Photo is going to dir2,
your solution is copying all to dir1, so doesn't comply.

By the way:

Code:
cp -r /home/satimis/$i/ /home/satimis/Temp/dir1/
is doing the very same operation that
Code:
/home/satimis/$i /home/satimis/Temp/dir1/
assuming $i is a directory, I dont see a point here.
 
Old 10-25-2004, 02:50 AM   #9
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Yep your right and I see now about this being pointless. Maybe it was meant to be:

cp -r /home/satimis/$i/ /home/satimis/Temp/

Then maybe there is a point.

Last edited by micxz; 10-25-2004 at 02:53 AM.
 
Old 10-25-2004, 03:09 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Nope, the same.

The only difference in using a trailing slash is that if $i is a file, an error is triggered and the file is not copied, while with no slash, whether it is a file or a directory doesn't matter, it is copied to the destination directory.

For the first remark, I was not arguing of using a separate file for the directory list, which can be a good idea, especially if the file itself need to created by another program, but about the extra level of directories necessary to avoid same name files to be overwritten.
 
Old 10-25-2004, 03:57 AM   #11
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Original Poster
Rep: Reputation: 56
Hi folks,

Lot of thanks for your suggestions. Hereinunder is the complete script
Code:
!/bin/bash

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

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

read -p "Add Photo directory <yes/no>"
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 -l -graft-points -hide-rr-moved \
$dir1 \
$dir2 \
$list |\
cdrecord dev=ATA:1,0,0 -v -eject -

# Delete directories and files...
rm -r /home/satimis/Temp/dir1/* /home/satimis/Temp/dir2/*
dir1, dir2…. etc. are only transit directories. The files copied to there will be removed finally after the running of the whole script.

Actually this is not a good arrangement. But I’m not allowed to touch the “cdrecord” command
Code:
 # Create ISO file...
mkisofs -R -l -graft-points -hide-rr-moved \
$dir1 \
$dir2 \
$list |\
cdrecord dev=ATA:1,0,0 -v -eject –
I need a further confirmation before adding dir1, dir2, dir3…. etc. each to the ISO file.

Have any other suggestions? TIA

B.R.
satimis
 
Old 10-25-2004, 05:59 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Some comments:

you call the cancel command if the user is not willing to add Document of Photo directories. This command's purpose is to cancel a printing job in progress, I'm quite sure it's not what you meant.

Why didn't you take or comment any of our suggestions about optimisation ?

You last loop isn't testing the validity of the directory entered.
 
Old 10-25-2004, 11:40 AM   #13
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

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


# This starts my modifications. Note this is untested!
cp_documents() {
cp -r /home/satimis/Document/* /home/satimis/Temp/dir1/
}

cp_pictures() {
cp -r /home/satimis/Pictures/* /home/satimis/Temp/dir1/
}

REPLY="B"
until [ $REPLY = "" ]
do
echo "Choose one of the following:"
echo
echo "[D]ocuments"
echo "[P]ictures"
echo "[B]oth"
echo
echo "[Enter] = Exit"
echo

read

case "$REPLY" in

"D" | "d" )
# Accept upper or lowercase input.
cp_documents
;;

"P" | "p" )
cp_pictures
;;

"B" | "b" )
cp_documents
cp_pictures
;;

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

esac
done


# The rest of the script is yours
# Create ISO file...
mkisofs -R -l -graft-points -hide-rr-moved \
$dir1 \
$dir2 \
$list |\
cdrecord dev=ATA:1,0,0 -v -eject -
 
Old 10-25-2004, 10:41 PM   #14
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Original Poster
Rep: Reputation: 56
Hi /bin/bash,

Tks for your advice.

Testing result is as follows;

Script
Code:
#!/bin/bash

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

# This starts my modifications. Note this is untested!
cp_documents() {
cp -r /home/satimis/Document/* /home/satimis/Temp/dir1/
}

cp_pictures() {
cp -r /home/satimis/Photo/* /home/satimis/Temp/dir1/
}


REPLY="B"
until [ $REPLY = "" ]
do
echo "Choose one of the following:"
echo
echo "[D]ocuments"
echo "[P]ictures"
echo "[B]oth"
echo
echo "[Enter] = Exit"
echo

read

case "$REPLY" in

"D" | "d" )
# Accept upper or lowercase input.
cp_documents
;;

"P" | "p" )
cp_pictures
;;

"B" | "b" )
cp_documents
cp_pictures
;;

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

esac
done

# 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 -l -o "$ISO_File" -graft-points -hide-rr-moved \
$dir1 \
$dir2 \
$list |\
cdrecord dev=ATA:1,0,0 -v -eject -
$ ./cdmaker
Code:
Choose one of the following:
[D]ocuments
[P]ictures
[B]oth

[Enter] = Exit

D
It hung here.

Remark: also tried P,p,B,b
same result

B.R.
satimis
 
Old 10-25-2004, 11:36 PM   #15
nitin_batta
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15
Quote:
REPLY="B"
until [ $REPLY = "" ]
do
echo "Choose one of the following:"
echo
echo "[D]ocuments"
echo "[P]ictures"
echo "[B]oth"
echo
echo "[Enter] = Exit"
echo

read

case "$REPLY" in

"D" | "d" )
# Accept upper or lowercase input.
cp_documents
;;

"P" | "p" )
cp_pictures
;;

"B" | "b" )
cp_documents
cp_pictures
;;
You have to user read REPLY on place of just read so that variable read is assigned a value.

Code:
echo

read REPLY

case "$REPLY" in

"D" | "d" )
 
  


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
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM
Bash Script; command not found twintornado Programming 2 06-01-2005 09:59 AM
How to get linux distribution with command bash or script gigix Linux - Distributions 5 04-28-2005 06:17 AM
Simple Bash Script Help Command kemplej Linux - Software 1 03-11-2004 03:52 AM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM

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

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