Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 20, 2012, 10:42 AM   #26
jamesadrian
Thread Starter
macrumors member
 
Join Date: Aug 2010
Quote:
Originally Posted by peoplevoice View Post
Adrian,

The below statement is giving you access to all and each one of the files in the input directory ($1), except directories. Not only the first file as you have been thinking.
--------------------------------------------------------------
for inputFile in `ls -l $1 | grep "^-" | awk '{print $(NF)}'`
do
---------------------------------------------------------------


Your C program redirects the standard input and output through < and > symbols. So inside your C program you don't need to open and close files. This is the reason I say the program doesn't need to know anything about filenames.
---------------------------------------------------------------------------
if yourCProgram < $1/$inputFile > $2/$inputFile
---------------------------------------------------------------------------


If the execution of your C program is okay the file in progress is removed.
----------------------------------------------------------------------------------
then rm $1/$inputFile
----------------------------------------------------------------------------------



Putting everything together

for inputFile in `ls -l $1 | grep "^-" | awk '{print $(NF)}'`
do
if yourCProgram < $1/$inputFile > $2/$inputFile
then rm $1/$inputFile
fi
done

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"
James Adrian
jim@futurebeacon.com

Last edited by jamesadrian; Oct 20, 2012 at 12:01 PM. Reason: code tags
jamesadrian is offline   0 Reply With Quote
Old Oct 20, 2012, 01:40 PM   #27
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by jamesadrian View Post
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:
Please describe what the script is expected to do. We could guess, but it's much clearer if you tell us what it should do.

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
In this loop, what do you intend the red-hilited break to do? Here's what the bash reference says it does:
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
chown33 is offline   0 Reply With Quote
Old Oct 20, 2012, 02:31 PM   #28
jamesadrian
Thread Starter
macrumors member
 
Join Date: Aug 2010
Quote:
Originally Posted by chown33 View Post
Please describe what the script is expected to do. We could guess, but it's much clearer if you tell us what it should do.

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
In this loop, what do you intend the red-hilited break to do? Here's what the bash reference says it does:
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.
The script I call fileloop above is supposed to move the files in a directory to another directory. I place both of these directories in my home directory and invoked the the program with this command: ./fileloop

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
jamesadrian is offline   0 Reply With Quote
Old Oct 20, 2012, 03:35 PM   #29
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by jamesadrian View Post
The script I call fileloop above is supposed to move the files in a directory to another directory. I place both of these directories in my home directory and invoked the the program with this command: ./fileloop

I had previously entered chmod 777 fileloop.

The program does not move any files.
Your shell script contains this line:
Code:
cd /tmp
What do you think this does?

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
Looking at the "after looping through files" output, it clearly shows dir1 containing f2, and dir2 containing f1. If you compare that to the "before looping", it plainly has moved one file: f1.

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:
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.
Yes, it is wrong. The question I have to ask is this: Why did you expect the program to stop and display something until a key is pressed? That isn't even remotely close to what the description for 'break' says.
chown33 is offline   0 Reply With Quote
Old Oct 25, 2012, 06:02 AM   #30
jamesadrian
Thread Starter
macrumors member
 
Join Date: Aug 2010
Quote:
Originally Posted by chown33 View Post
Your shell script contains this line:
Code:
cd /tmp
What do you think this does?

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
Looking at the "after looping through files" output, it clearly shows dir1 containing f2, and dir2 containing f1. If you compare that to the "before looping", it plainly has moved one file: f1.

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.



Yes, it is wrong. The question I have to ask is this: Why did you expect the program to stop and display something until a key is pressed? That isn't even remotely close to what the description for 'break' says.

If you make the price of failure high enough, nobody will try.

I will now try to hire a consultant.

James Adrian
jamesadrian is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Help appending file names? tobefirst Mac Programming 11 Feb 13, 2014 03:40 PM
Finder Not Searching Folders for File Names or File Content HoberMallow Mac mini 1 Sep 30, 2013 11:47 AM
File Names in Finder jerirat Mac Basics and Help 4 Aug 21, 2013 08:52 PM
cannot rename file names of folders, files, or change case of file extensions Sossity Mac Basics and Help 8 Apr 2, 2013 05:38 AM
Can't change file names AT ALL garygibsonsf Mac Basics and Help 2 Jun 6, 2012 07:22 AM

Forum Jump

All times are GMT -5. The time now is 01:43 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC