While loop help - Bash Script

BerGaur

macrumors regular
Original poster
Dec 12, 2011
108
0
Inside my head
I made a script that manipulates a text file. The idea is to manipulate it, open it, then manipulate it one more time after I close the directory it is in.

I used a While loop to hold the activity open as long as that directory is open, so when it is false the script will finnish the manipulation.

My problem is that the while loop runs for a second, then fails and says that it has too many arguments. So, here it is:

StayOpen=`lsof | grep /Path/to/FolderName | rev | cut -c1-10 | rev`
while [ $StayOpen = "FolderName" ]; do
StayOpen=`lsof | grep /Path/to/FolderName | rev | cut -c1-10 | rev`
done
 

BerGaur

macrumors regular
Original poster
Dec 12, 2011
108
0
Inside my head
Solution Found!

I have discovered my problem!

My problem was the fact that I didn't consider aspects of how the lsof command works. When my script executed:

StayOpen=`lsof | grep /Path/to/FolderName | rev | cut -c1-10 | rev`
while [ $StayOpen = "FolderName" ]; do
StayOpen=`lsof | grep /Path/to/FolderName | rev | cut -c1-10 | rev`
done

It would put FolderName inside $StayOpen. Then, as bash used this script that was held open the lsof command would show more /Path/to/FolderName lines, and grep would also through in a few as well. Here is an example of the lsof command in use:

Finder 74682 home 11r DIR 1,3 204 2083863 /Path/to/FolderName
bash 77513 home cwd DIR 1,3 204 2083863 /Path/to/FolderName
bash 77513 home 255r REG 1,3 1618 2091009 /Path/to/FolderName/TheScript.tool
bash 77823 home cwd DIR 1,3 204 2083863 /Path/to/FolderName
lsof 77824 home cwd DIR 1,3 204 2083863 /Path/to/FolderName
grep 77825 home cwd DIR 1,3 204 2083863 /Path/to/FolderName
grep 77826 home cwd DIR 1,3 204 2083863 /Path/to/FolderName
grep 77827 home cwd DIR 1,3 204 2083863 /Path/to/FolderName
lsof 77828 home cwd DIR 1,3 204 2083863 /Path/to/FolderName

One would indicate that it was through the Finder Application, while the others would indicate that it was bash that was using them. The ONE that said Finder is the actual directory.

So, $StayOpen would become, "FolderName FolderName FolderName FolderName FolderName FolderName FolderName FolderName". Thus the error: too many arguments.

My solution was to pipe it through grep a couple more times so I can pinpoint it as being used by Finder and, to cover as many basses as I could, to specify that it is a directory. Then I reversed my approach so I could have yet another fail safe:

StayOpen=`lsof | grep /Path/to/FolderName | grep Finder | grep DIR`
while [ $StayOpen != "" ]
do
StayOpen=`lsof | grep /Path/to/FolderName | grep Finder | grep DIR`
done

This extra fail safe (!=) now needs a specific value to close, rather than a specific value to open. So if something throws it off, now it be kept open, rather than find a different value and close because it didn't mach what it needs to stay open. So, I can do my work they way I want, and then, if something screws up and keeps it open, I simply click on the terminal and hit Control+C to close it.
 
Last edited:

seco

macrumors newbie
May 16, 2018
2
0
Bash loops still tricky for many people especially beginners.
I searched a lot about for and while loops. This article https://likegeeks.com/bash-scripting-step-step-part2/ helped me a lot too.
For me, the C-style makes easy to write loops without errors.
I have no idea why we need to write 2 brackets while using for loops like in the mentioned article!!
for (( v1 = 1; v1 <= 3; v1++ ))