Hello I am learning Scripting here. I am trying to write a simple script using the 'for' loop.
I have hundreds of folders in a folder called user.
if i run this command i get a list of folders that i need to move to another folder
cat failed | awk -F ":" '/FAILED/ {print $1}' | uniq
i.e folders under users that have failed a task have to be moved to users/failed/failedusers
what i am currently doing is i am creating a new file first using
cat failed | awk -F ":" '/FAILED/ {print $1}' | uniq > failedusers
and then i move the folders using the following command
while read line; do cp -r users/$line users/failed; done < failedusers
My question here is can i do the same using just the for command instead of writing the output to another file and using the while read command to get it done?
for example somehow assign a value to a variable in a loop like
faileduser=`cat failed | awk -F ":" '/FAILED/ {print $1}' | uniq`
and then write something like
mv users/$faileduser user/failed/$faileduser
i am getting all kinds of errors when i am trying to write something like above.
thanks
for
comes into the picture, but do you mean something likecat failed | awk -F ":" '/FAILED/ {print $1}' | uniq | while read line; do cp -r users/$line users/failed; done < failedusers
? If so you could usexargs
withcp
instead of usingwhile
+read
+cp
– muru Jun 14 '22 at 11:45