|
|
#26 | |
|
Quote:
With a lot of help for all of you and some others, I have been working on a script that takes the other approach. It does not seem to work yet, but here it is: Code:
#!/bin/bash
# I discovered this path by entering this command: which bash
# setup test dirs and files
d1="dir1"
d2="dir2"
cd /tmp
rm -rf $d1
rm -rf $d2
mkdir $d1
touch $d1/f1
touch $d1/f2
mkdir $d2
# define function
function func1
{
fname=$1
echo "in func1, parm fname=$fname"
}
# show current dir state
echo
echo "before looping through files"
echo "------------------------"
echo "$d1 contents"
ls $d1
echo "$d2 contents"
ls $d2
echo
echo "looping through files in $d1"
echo "------------------------"
# loop through files in d1
for f in $(ls -1 $d1)
do
# call function
func1 $f
# move file to d2
mv $d1/$f $d2/
break
done
# confirm results of moved files
echo
echo "after looping through files"
echo "------------------------"
echo "$d1 contents"
ls $d1
echo "$d2 contents"
ls $d2
echo "------------------------"
echo "done"
jim@futurebeacon.com Last edited by jamesadrian; Oct 20, 2012 at 12:01 PM. Reason: code tags |
||
|
|
0
|
|
|
#27 | |
|
Quote:
Rules of Thumb: 1. Describe what you expected to happen. 2. Describe what actually happened. Code:
# loop through files in d1
for f in $(ls -1 $d1)
do
# call function
func1 $f
# move file to d2
mv $d1/$f $d2/
break
done
Exit from within a for, while, until, or select loop.If that's what you intend, then you should explain what the overall intent of the loop is. If that's not what you intend, then you should ask yourself why it's there at all. Last edited by chown33; Oct 20, 2012 at 01:57 PM. Reason: fix typos |
||
|
|
0
|
|
|
#28 | |
|
Quote:
I had previously entered chmod 777 fileloop. The program does not move any files. Thank you for your help. I had expected the break command to stop the program and display something until a key press starts it up again. I guess that is wrong. I tried it without the break command and got the same result. No files were move. James Adrian jim@futurebeacon.com James Adrian jim@futurebeacon.com Last edited by jamesadrian; Oct 20, 2012 at 02:39 PM. Reason: left out an answer |
||
|
|
0
|
|
|
#29 | ||
|
Quote:
Code:
cd /tmp It's very important that you understand everything you put into your script. If you don't understand what you're telling the script to do, you really shouldn't be surprised if it does something you don't understand. It can't read your mind. It does exactly what you tell it to do, even if you don't understand what you told it. When I run your shell script exactly as posted (i.e. with the 'break' in the loop), it produces this output: Code:
before looping through files ------------------------ dir1 contents f1 f2 dir2 contents looping through files in dir1 ------------------------ in func1, parm fname=f1 after looping through files ------------------------ dir1 contents f2 dir2 contents f1 ------------------------ done But exactly where are the directories dir1 and dir2 located? The answer requires understanding the line I pointed out above. When I look in the correct location, the directories dir1 and dir2 exist, and contain exactly the files that the output says they do. So the problem isn't that the script isn't working. The problem seems to be that you don't understand what you told the script to do. Quote:
|
|||
|
|
0
|
|
|
#30 | |
|
Quote:
If you make the price of failure high enough, nobody will try. I will now try to hire a consultant. James Adrian |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 09:08 PM.







Linear Mode
