Bash script - repeated command
Hi folks,
I have a script which needs following command to repeat confirmation on questions. Code:
...... TIA B.R. satimis |
y dont' you just use the while loop??
|
Just make a seperate file having the questions and then use the script as
Code:
for i in `cat q_file` |
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` 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? |
But micxz is missing the dir1, dir2, ... requirement, what about:
Code:
i=0 |
Quote:
|
Quote:
is the same as for i in Document Photo..... In this example, so how am I missing dir1 and dir2... |
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/ Code:
/home/satimis/$i /home/satimis/Temp/dir1/ |
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. |
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. |
Hi folks,
Lot of thanks for your suggestions. Hereinunder is the complete script Code:
!/bin/bash Actually this is not a good arrangement. But I’m not allowed to touch the “cdrecord” command Code:
# Create ISO file... Have any other suggestions? TIA B.R. satimis |
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. |
#!/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 - |
Hi /bin/bash,
Tks for your advice. Testing result is as follows; Script Code:
#!/bin/bash Code:
Choose one of the following: Remark: also tried P,p,B,b same result B.R. satimis |
Quote:
Code:
echo |
Hi /bin/bash,
Further to my last posting. Sorry I made a mistake. Your script works. Because of the size of the directories I have to wait. Testing result is as follows; Script Code:
#!/bin/bash Code:
Choose one of the following: worked copying files to dir1/dir2 respectively but can't create ISO file. After hitting >ENTER> Code:
./cdmaker: line 21: [: =: unary operator expected all files copied to dir1 from [D]ocuments and [P]ictures directories B.R. satimis |
Code:
cp -r /home/satimis/Photo/* /home/satimis/Temp/dir1/ Code:
cp -r /home/satimis/Photo/* /home/satimis/Temp/dir2/ Code:
if [ $entry != "" ]; then Code:
if [ $entry != "" ]; then |
Hi jlliagre,
Tks for your advice. Correction made. Script Code:
#!/bin/bash Choose one of the following: [D]ocument [P]hoto [B]oth [Enter] = Exit f Choose one of the following: [D]ocument [P]hoto [B]oth [Enter] = Exit p Choose one of the following: [D]ocument [P]hoto [B]oth [Enter] = Exit It hung here after hitting <ENTER>. Files copied to dir1 and dir2 respectively but can't create ISO file. B.R. satimis |
Code:
echo "Enter directories to burn. Leave blank to end list" Code:
echo "Enter directories to burn. Leave blank to end list" |
Quote:
The problem is with the until test. It needs to be changed like this. until [ $REPLY = "" ] s/b until [ z$REPLY = "z" ] Sorry about that. But I did say it was untested. |
Hi /bin/bash,
Quote:
Could you please help me to understand the function of "z" TIA B.R. satimis |
The ugly way to handle empty strings:
Quote:
Quote:
Quote:
|
Quote:
Both suggestions of yours worked for me here. I don't understand the function of "z", could you please explain it. TIA B.R. satimis |
Try reading, mate :) Has always worked miracles for me ;}
http://www.tldp.org/LDP/abs/html/testconstructs.html Cheers, Tink |
The z is prefixing the variable, so if the variable is empty, the test if [ z = "z" ] is true.
Without the z on both sides, the test would become if [ = "" ] which is a syntax error. Any character or string can be used instead of z, but anyway, this is a very poor programming technique, as the variable can be put between double quotes to avoid the shell complaining. |
Quote:
Noted with thanks B.R. satimis |
Quote:
Tks for your link. Sorry, I did not catch "Try reading, mate Has always worked miracles for me" B.R. satimis |
The nice thing about helping people for free is that no one expects you to sit down and write perfect code, using only "good programming technique." Well usually thats true. :rolleyes:
Anyways satimis, if you are ever in a hurry, you don't have time for debugging and you forget what the test is for empty string, you at least know how to do it the UGLY way. ;) |
All times are GMT -5. The time now is 09:26 AM. |