Repeat command with new variable for each line in txt file

Well here is my question.

Let's say I have this Script:

find /var/mobile/ maxdepth -2 name "$x" >> /"$x".txt

The thing is I want to repeat this script and pull out a variable from a text file like this (each line = new variable $x and another run of the whole command)

Thanks for any replies in advance.

while read x
 
do
....$x...
done <file
1 Like

do I have to use brackets to make this work ?

like:

while read x do (
find /var/mobile/ maxdepth -2 name "$x" >> /"$x".txt
done < /var/mobile/names.txt)

??

If not: Mind explaining this further (perhaps showing directly how to apply this ?

Thanks.

Less is more:

while read x 
do
 find /var/mobile -maxdepth 2 -name "$x" >> "$x".txt
done < /var/mobile/names.txt
1 Like

Thanks, thats just perfectly fine !

---------- Post updated at 06:18 PM ---------- Previous update was at 03:30 PM ----------

Another question.

If I use this

while read x
do
printf "cp /path/to/app.app/folder/$x.plist" >> /var/mobile/z_folder/"$x".sh
done < /var/mobile/names.txt

it outputs this (into the "$x".sh file):

cp /path/to/app.app/folder/.plist 

Ergo, the variable is not pasted inside the file.

The file itself ("$x".sh) is just named fine. (In this case Name1, reffering to the first post).

Why is the variable just swallowed like this ?

That can't be the code you are using. There's a glaring syntax error in the printf statement.

My guess is what you have looks more like this:

printf 'cp /path/to/app.app/folder/$x.plist' >> /var/mobile/z_folder/"$x".sh

This would fit with the behaviour you are describing.

Exactly ! However what is wrong with that code ?

why does the x$ inside the newly created file not expand to Name1 (in this case) ?

Sorry, I should have said! Because it's in single quotes. It needs to be in double quotes if the shell is to expand it.

So... like this:printf "cp /path/to/app.app/folder/$x.plist" >> /var/mobile/z_folder/"$x".sh
this:printf "cp /path/to/app.app/folder/"$x".plist" >> /var/mobile/z_folder/"$x".sh
or this?:
printf "cp /path/to/app.app/folder/""$x"".plist" >> /var/mobile/z_folder/"$x".sh
??
I think the first two didn't work for me... do they for you ?

The first one is fine:

printf "cp /path/to/app.app/folder/$x.plist\n" >> /var/mobile/z_folder/"$x".sh

is fine. Not forgetting the newline.

Actually, I did not read the command properly!

printf "cp /path/to/app.app/folder/'$x'.plist >> /var/mobile/z_folder/'$x'.sh\n"

But all this does is print it to the screen, without copying any files, or storing the copy commands. Is that what you wanted?

Seems like 'echo "...$...." >>...$....sh' would suffice. However, if you want to run them, then run them, not write them. Seems like a case of tweeser-itis, playing withyour food! :smiley: http://en.wikiquote.org/wiki/The\_Good,\_the\_Bad\_and\_the_Ugly\#Tuco

The first two ways don't work, because the first $x is expanded. The third also does not work, and is quite confusing. To make it clear and easy to understand, the two best ways to write this are:

printf 'cp /path/to/app.app/folder/$x.plist\n' >> "/var/mobile/z_folder/$x.sh"
echo   'cp /path/to/app.app/folder/$x.plist'   >> "/var/mobile/z_folder/$x.sh"

Agree with dgpickett. If you ever find yourself

1) generating code
2) using eval for basic, simple things
3) building shell scripts with shell scripts

...chances are you've missed or skipped the obvious way.

Creating 10,000 nearly-identical scripts with slightly different variables? Why not have it take a parameter, then call the same one 10,000 times? In fact, if it's a script with one line, why call a script at all?

Generating code and storing it for later? Why not generate it later and avoid the storing, or just store a list of files instead of an entire script?

In fact, why not just copy then and there, now or later, without generating code? The shell's fully capable of doing all these things.

Neither of your suggestions make any sense.

What's the point of writing

cp /path/to/app.app/folder/$x.plist

to

/var/mobile/z_folder/$x.sh

?

---

I agree too for the most part.

All of the suggestions posted above share one and the same problem (at least when I use them with my shell environment):
One of the two variables doesn't get extracted.

Results:
1st doesn't get extracted: swallowed line or part that contains variable
2nd doesn't get extracted: all paths are written into a $x.sh file that magically contains all paths with the variables extracted.

I'd gladly listen to further ideas regarding this.

Till then:

I went another route to achieve what I want:

while read x
do
  printf "cp /path/to/app/xtzdero204.plist " >> /var/mobile/z_renapps/"$x".sh
  sed -i "s|xtzdero204|$x|g" /var/mobile/z_renapps/"$x".sh
done < /Applications/RenApple.app/applist.txt

You could do it all in sed, no printf, using the 'w filename' to make a copy of the line in the file.

Use printf more for formatting numbers in complex strings. The echo is cheaper for simple strings.

We still do not understand why you wish to make 10,000 individual scripts with slight differences. This kind of "need" usually points to a poor understanding of the shell.

You are probably right about this.

I merge the multiple shells back into one in the next step.
It could probably be acheived in a simpler manner.

On a different standing:

it seems that

while read x
do
  CODE
done < /TEXT.txt

will only process 52 lines of this text file.
It always stops after the 52's line.

I shall post the full Script I have when someone can help me with understanding why it stops exactly after 52 repetitions.

Well, assuming there are more lines, the answer is that the 52nd line $x poisoned CODE and it hung. Put an echo "x='$x'" before CODE to make sure which line in toxic.