How to write a shell script that creates empty files with all possible permissions. File names should be, for example, rwxrw_r__.txt. I know how to do it manually. As an example:
#!/bin/bash
touch task5/rwxrwxrwx.txt | chmod 777 task5/rwxrwxrwx.txt
touch task5/rwxr-xr-x.txt | chmod 755 task5/rwxr-xr-x.txt
touch task5/rwx------.txt | chmod 700 task5/rwx------.txt
touch task5/rw-rw-rw-.txt | chmod 666 task5/rw-rw-rw-.txt
touch task5/rw-r--r--.txt | chmod 644 task5/rw-r--r--.txt
touch task5/rw-------.txt | chmod 600 task5/rw-------.txt
I do not know how to write a script that will create files according to the required template and give them permissions
CodePudding user response:
Do a loop:
for ((i=0; i < 512; i )); do
mod=$(printf "o\n" "$i");
touch ${mod}.txt; chmod $mod $mod.txt;
done
Rather than trying to construct the names, if you want the names to look like the output of ls -l
, just do something like
for ((i=0; i < 512; i )); do
mod=$(printf "o\n" "$i")
touch ${mod}.txt
chmod $mod $mod.txt
n=$(ls -l $mod.txt | awk '{print $1}')
mv $mod.txt $n.txt
done
CodePudding user response:
It's just a permutations problem.
p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
for u in "${p[@]}"; do for g in "${p[@]}"; do for o in "${p[@]}"; do
f="task5/$u$g$o.txt"; touch -- "$f" && chmod "u=$u,g=$g,o=$o" -- "$f"
done; done; done
This took a little over a minute on my laptop.