如何在脚本中遍历某些文件?

时间:2021-11-27 01:13:50

I am very much a beginner at this and have searched for answers my question but have not found any that I understand how to implement. Any help would be greatly appreciated.

我是一个初学者,我已经搜索了我的问题的答案,但没有找到任何我理解如何实现。如有任何帮助,我们将不胜感激。

I have a script:

我有一个脚本:

FILE$=`ls ~/Desktop/File_Converted/`
mkdir /tmp/$FILE
mv ~/Desktop/File_Converted/* /tmp/$FILE/

So I can use Applescript to say when a file is dropped into this desktop folder, create a temp directory, move the file there and the do other stuff. I then delete the temp directory. This is fine as far as it goes, but the problem is that if another file is dropped into File_Converted directory before I am doing doing stuff to the file I am currently working with it will change the value of the $FILE variable before the script has completed operating on the current file.

因此我可以使用Applescript技术来说明文件何时被放入这个桌面文件夹,创建一个临时目录,将文件移动到那里,然后做其他的事情。然后删除临时目录。就现状来说这是好的,但问题是如果另一个文件是掉进File_Converted目录文件之前我做的做的东西我现在使用它将改变的价值$文件变量在脚本操作对当前文件完成。

What I'd like to do is use a variable set up where the variable is, say, $FILE1. I check to see if $FILE1 is defined and, if not, use it. If it is defined, then try $FILE2, etc... In the end, when I am done, I want to reclaim the variable so $FILE1 get set back to null again and the next file dropped into the File_Converted folder can use it again.

我想做的是使用一个变量设置在变量的位置,比如$FILE1。我检查是否定义了$FILE1,如果没有,则使用它。如果它被定义,那么尝试$FILE2,等等…最后,当我完成时,我想要重新获取变量,这样$FILE1将再次被设置为null,然后下一个被放入file_conversion文件夹的文件可以再次使用它。

Any help would be greatly appreciated. I'm new to this so I don't know where to begin.

如有任何帮助,我们将不胜感激。我是新手,所以我不知道从哪里开始。

Thanks!

谢谢!

Dan

3 个解决方案

#1


2  

Your question is a little difficult to parse, but I think you're not really understanding shell globs or looping constructs. The globs are expanded based on what's there now, not what might be there earlier or later.

您的问题有点难以解析,但我认为您并没有真正理解shell globs或循环结构。globs的扩展基于现在的情况,而不是之前或之后的情况。

DIR=$(mktemp -d)
mv ~/Desktop/File_Converted/* "$DIR"
cd "$DIR"
for file in *; do
    : # whatever you want to do to "$file"
done

#2


2  

You don't need a LIFO -- multiple copies of the script run for different events won't have conflict over their variable names. What they will conflict on is shared temporary directories, and you should use mktemp -d to create a temporary directory with a new, unique, and guaranteed-nonconflicting name every time your script is run.

您不需要LIFO——针对不同事件运行的脚本的多个副本不会因其变量名而产生冲突。它们将冲突的是共享的临时目录,您应该使用mktemp -d创建一个临时目录,在每次运行脚本时使用新的、惟一的和保证不冲突的名称。

tempdir=$(mktemp -t -d mytemp.XXXXXX)
mv ~/Desktop/File_Converted/* "$tempdir"
cd "$tempdir"
for f in *; do
  ...whatever...
done

#3


-1  

What you describe is a classic race condition, in which it is not clear that one operation will finish before a conflicting operation starts. These are not easy to handle, but you will learn so much about scripting and programming by handling them that it is well worth the effort to do so, even just for learning's sake.

您所描述的是一个典型的竞态条件,其中一个操作在冲突操作开始之前是否会完成尚不清楚。这些并不容易处理,但是通过处理它们,您将学到很多关于脚本和编程的知识,因此,即使只是为了学习,这样做也是值得的。

I would recommend that you start by reviewing the lockfile or flock manpage. Try some experiments. It looks as though you probably have the right aptitude for this, for you are asking exactly the right questions.

我建议您从检查lockfile或flock manpage开始。尝试一些实验。看起来你似乎有做这件事的正确资质,因为你问的正是正确的问题。

By the way, I suspect that you want to kill the $ in

顺便说一下,我猜你是想把钱拿走

FILE$=`ls ~/Desktop/File_Converted/`

Incidentally, @CharlesDuffy correctly observes that "using ls in scripts is indicative of something being done wrong in and of itself. See mywiki.wooledge.org/ParsingLs and mywiki.wooledge.org/BashPitfalls." One suspects that the suggested lockfile exercise will clear up both points, though it will probably take you several hours to work through it.

顺便说一句,@CharlesDuffy正确地观察到“在脚本中使用ls表明在自身和自身中发生了错误。”看到mywiki.wooledge.org/ParsingLs mywiki.wooledge.org/BashPitfalls。”有人怀疑,建议的lockfile练习可以解决这两个问题,尽管可能需要几个小时才能完成。

#1


2  

Your question is a little difficult to parse, but I think you're not really understanding shell globs or looping constructs. The globs are expanded based on what's there now, not what might be there earlier or later.

您的问题有点难以解析,但我认为您并没有真正理解shell globs或循环结构。globs的扩展基于现在的情况,而不是之前或之后的情况。

DIR=$(mktemp -d)
mv ~/Desktop/File_Converted/* "$DIR"
cd "$DIR"
for file in *; do
    : # whatever you want to do to "$file"
done

#2


2  

You don't need a LIFO -- multiple copies of the script run for different events won't have conflict over their variable names. What they will conflict on is shared temporary directories, and you should use mktemp -d to create a temporary directory with a new, unique, and guaranteed-nonconflicting name every time your script is run.

您不需要LIFO——针对不同事件运行的脚本的多个副本不会因其变量名而产生冲突。它们将冲突的是共享的临时目录,您应该使用mktemp -d创建一个临时目录,在每次运行脚本时使用新的、惟一的和保证不冲突的名称。

tempdir=$(mktemp -t -d mytemp.XXXXXX)
mv ~/Desktop/File_Converted/* "$tempdir"
cd "$tempdir"
for f in *; do
  ...whatever...
done

#3


-1  

What you describe is a classic race condition, in which it is not clear that one operation will finish before a conflicting operation starts. These are not easy to handle, but you will learn so much about scripting and programming by handling them that it is well worth the effort to do so, even just for learning's sake.

您所描述的是一个典型的竞态条件,其中一个操作在冲突操作开始之前是否会完成尚不清楚。这些并不容易处理,但是通过处理它们,您将学到很多关于脚本和编程的知识,因此,即使只是为了学习,这样做也是值得的。

I would recommend that you start by reviewing the lockfile or flock manpage. Try some experiments. It looks as though you probably have the right aptitude for this, for you are asking exactly the right questions.

我建议您从检查lockfile或flock manpage开始。尝试一些实验。看起来你似乎有做这件事的正确资质,因为你问的正是正确的问题。

By the way, I suspect that you want to kill the $ in

顺便说一下,我猜你是想把钱拿走

FILE$=`ls ~/Desktop/File_Converted/`

Incidentally, @CharlesDuffy correctly observes that "using ls in scripts is indicative of something being done wrong in and of itself. See mywiki.wooledge.org/ParsingLs and mywiki.wooledge.org/BashPitfalls." One suspects that the suggested lockfile exercise will clear up both points, though it will probably take you several hours to work through it.

顺便说一句,@CharlesDuffy正确地观察到“在脚本中使用ls表明在自身和自身中发生了错误。”看到mywiki.wooledge.org/ParsingLs mywiki.wooledge.org/BashPitfalls。”有人怀疑,建议的lockfile练习可以解决这两个问题,尽管可能需要几个小时才能完成。