I asked a similar question before, but the answers that I got weren't exactly what I was looking for. I want to create 100 .txt files with 1 random number in each of those files and assign them permissions based on the number inside the file. I used:
#!/bin/bash
rm -r -f .txt
touch {1..100}.txt
r=$RANDOM
to create new files each time the script is ran and assigned random number to "r". How do I paste a different number into each file. I'm not sure if I need to use echo or shuf to do it.
After that I need to check if the random number is divisible by 2 and 3 and give the files permissions based on that. I intend to use:
chmod 000 {1..100}.txt
if ((r % 2 == 0))
then echo chmod rw {1..100}.txt
elif ((r % 3 ==0))
then chmod 777 {1..100}.txt
else chmod w {1..100}
but I'm not sure if that works either.
CodePudding user response:
What's a simple way to create 100 .txt
Iterate over files and output the numbers.
for ((i = 1; i <= 100; i)); do
echo "$RANDOM" > "$i.txt"
done
CodePudding user response:
Using chmod
can be done with code similar to the code in your question (changing it for 1 file each time and adding a fi
). I skipped the chmod 000
and assign the correct mode at once.
for ((i = 1; i <= 100; i)); do
r="$RANDOM"
echo "$r" > "$i.txt"
if ((r % 2 == 0)); then
chmod 666 "$i.txt"
elif ((r % 3 ==0)); then
chmod 777 "$i.txt"
else
chmod 222 "$i.txt"
fi
done