unix 用于创建具有所有可能权限的空文件的shell脚本

bxgwgixi  于 2022-11-04  发布在  Unix
关注(0)|答案(3)|浏览(279)

如何编写一个shell脚本,创建具有所有可能权限的空文件。文件名应该是,例如,rwxrw_r__. txt。我知道如何手动完成。例如:

  1. # !/bin/bash
  2. touch task5/rwxrwxrwx.txt | chmod 777 task5/rwxrwxrwx.txt
  3. touch task5/rwxr-xr-x.txt | chmod 755 task5/rwxr-xr-x.txt
  4. touch task5/rwx------.txt | chmod 700 task5/rwx------.txt
  5. touch task5/rw-rw-rw-.txt | chmod 666 task5/rw-rw-rw-.txt
  6. touch task5/rw-r--r--.txt | chmod 644 task5/rw-r--r--.txt
  7. touch task5/rw-------.txt | chmod 600 task5/rw-------.txt

我不知道如何编写一个脚本,该脚本将根据所需的模板创建文件并给予它们权限

kcugc4gi

kcugc4gi1#

执行循环:

  1. for ((i=0; i < 512; i++)); do
  2. mod=$(printf "%03o" "$i");
  3. touch ${mod}.txt; chmod $mod $mod.txt;
  4. done

如果您希望名称看起来像ls -l的输出,请执行以下操作,而不是尝试构造名称

  1. for ((i=0; i < 512; i++)); do
  2. mod=$(printf "%03o" "$i")
  3. touch ${mod}.txt
  4. chmod $mod $mod.txt
  5. n=$(ls -l $mod.txt | cut -b1-10)
  6. mv -- $mod.txt "$n.txt"
  7. done
xqkwcwgp

xqkwcwgp2#

这只是一个排列问题。

  1. p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
  2. for u in "${p[@]}"; do for g in "${p[@]}"; do for o in "${p[@]}"; do
  3. f="task5/$u$g$o.txt"; touch -- "$f" && chmod "u=${u//-/},g=${g//-/},o=${o//-/}" -- "$f";
  4. done; done; done

备注

  • 感谢@kvantour指出我被dashed传递到chmod,它不知道如何处理它们。我很惊讶我没有得到错误。

让我们把它分解一下,看看发生了什么。
如果您对权限集的含义或chmod的工作原理有任何疑问,请访问see here
因此,对于每个用户、组或其他,都有八种可能的符号表示(表示一个八进制数字0-7的值)。
我们将它们设置为一个简单的数组,可以循环访问。

  1. p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions

你可以访问任何一个八进制数的元素(技术上相当于十进制数,但这并不重要,除非你超过7),所以${p[5]}r-x。用@索引返回整个数组,所以循环用${p[@]}顺序遍历它们。
为了得到每个可能的排列,我们对每个用户/组/其他进行循环。

  1. for u in "${p[@]}"; do # assign each permission set for the user
  2. for g in "${p[@]}"; do # assign each permission set for the group
  3. for o in "${p[@]}"; do # assign each permission set for the other

这仅仅是嵌套循环中的简单迭代,以命中每个排列。

  1. f="task5/$u$g$o.txt" # assign the permissions string AS the filename

通过将路径和文件名信息放入一个变量中,我们可以在一个地方维护任何更改,这使得该行的其余部分更短,更容易阅读。

  1. touch -- "$f" && # create the file and test for success

touch将创建一个空文件,因为文件名 * 有时 * 会以破折号开始(任何时候权限不允许用户读取),我们给予touch一个--的第一个参数,这是一个 *NIX标准习惯用法,意思是“现在完成选项,剩下的都是参数”;否则它会试图将前导破折号解释为无效的选项集并失败。当您将“task 5/”放在文件名的开头时,这不会有问题,但如果您最终使用文件名bare,则会有问题。
&&是一个布尔测试,用于查看touch是否成功。如果没有成功,我们将自动跳过chmodtouch应该会为您的调试发出一条错误消息,但如果失败,您可能会收到大量错误消息,并且需要修复任何错误...)

  1. chmod "u=${u//-/},g=${g//-/},o=${o//-/}" -- "$f" # change the permissions

这里使用了chmod的符号模式。我们拥有下一个循环中每个部分的权限--只需要应用它们。同样,我们使用--来告诉chmod我们什么时候完成传递选项,这样如果你只把cd重构到目录中并在本地创建文件,文件名中的前导破折号就不会是一个问题。但即使这样,您也可以在其上添加前缀./$PWD/
我们必须去掉符号文件模式中的w破折号,因为(再次感谢@kvantour)chmod无法识别这些破折号。内联字符串编辑工作得很好:在"u=${u//-/},g=${g//-/},o=${o//-/}"中,变量规范中的//是所有出现的替换,将-替换为后面的/}之间的 nothing

  1. done; done; done # these just close each of the loops

我们可以(也可能应该)把每一行放在不同的行上,但解释器并不在意,因为我们使用了分号,它让我们压缩代码,把循环嵌套和闭包放在同一行上,只要你觉得改变的那一件事足够明显就行。
还有什么我没说的问题吗?

备选

另一个版本,因为我喜欢经历一次循环,而不是嵌套的shenannigans...

  1. p=( --- --x -w- -wx r-- r-x rw- rwx ) # the set of permissions
  2. for dec in {0..511}; do oct="$(printf "%03o" "$dec")"
  3. u="${p[${oct:0:1}]}"; g="${p[${oct:1:1}]}"; o="${p[${oct:2:1}]}";
  4. f="task5/$u$g$o.txt"; touch "$f"; chmod "u=${u//-/},g=${g//-/},o=${o//-/}" "$f";
  5. done

这将以数字方式遍历组合,使用printf将十进制转换为八进制,使用基本的子字符串解析将八进制权限集中的每一位数切片,并使用它从数组中查找相关的字符串,分配段,分配结果文件名,touch/创建文件,然后使用chmod应用清理后的权限字符串。虽然有点难以理解。

  1. u="${p[${oct:0:1}]}" # grabs 1 byte from offset 0 of $oct as index to $p

按照建议,要跳过十进制到八进制的转换步骤,请替换:

  1. for dec in {0..511}; do oct="$(printf "%03o" "$dec")"

  1. for oct in {0..7}{0..7}{0..7}; do
展开查看全部
wwodge7n

wwodge7n3#

根据Paul Hodges的回答-这是一个排列问题-但是,这种方法在内存数据库中使用bash和SQLite来生成一个受此网页启发的数据集,https://towardsdatascience.com/unix-permissions-the-easy-way-98cc19979b3e从八个记录中创建一个笛卡尔积-生成命令并将八进制与用户组所有权限集放在一起
chmod的八进制在此方法中进行了说明-这与原始请求更加一致

  1. # !/bin/bash
  2. WorkingFolder=task5
  3. RunnerFile=_T5Builder.bash
  4. rm -Rf ${WorkingFolder} ${RunnerFile}
  5. mkdir ${WorkingFolder}
  6. sqlite3 << FIN
  7. .headers off
  8. .mode column
  9. .once ${RunnerFile}
  10. with cte_elements as (
  11. select '0' as Id, '000' as Octal, '---' as Permission union
  12. select '1', '001', '--x' union select '2', '010', '-w-' union
  13. select '3', '011', '-wx' union select '4', '100', 'r--' union
  14. select '5', '101', 'r-x' union select '6', '110', 'rw-' union
  15. select '7', '111', 'rwx'
  16. )
  17. , cte_allpermissions as (
  18. select cte_elements.Id || cte_elements1.Id || cte_elements2.Id as Dec
  19. , cte_elements.Octal || cte_elements1.Octal || cte_elements2.Octal as OctFull
  20. , cte_elements.Permission || cte_elements1.Permission || cte_elements2.Permission as Permission
  21. from -- cartesian product
  22. cte_elements cross join cte_elements cte_elements1 cross join cte_elements cte_elements2 --cartesian product
  23. )
  24. , cte_constructor as (
  25. select 'touch ${WorkingFolder}/' || Dec || '_' || Permission || '.txt;' as "#TouchCommand"
  26. , 'chmod ' || Dec || ' ${WorkingFolder}/' || Dec || '_' || Permission || '.txt ;' as ChmodCommand
  27. , 'echo -n "' || Dec || ' "' as Progress
  28. from cte_allpermissions
  29. )
  30. select * from cte_constructor;
  31. FIN
  32. chmod +x ${RunnerFile}
  33. echo Top And Tail of Runner File ${RunnerFile}
  34. head -2 ${RunnerFile}
  35. tail -2 ${RunnerFile}
  36. source ${RunnerFile}
  37. echo
  38. echo Done
  39. ls -l ${WorkingFolder} | awk '{print $1, $9}' | more

输出如下所示(如果性能确实存在问题,则大约需要40秒

  1. MyNode:~/viasql$ time ./CreateAllPermFiles.bash
  2. Top And Tail of Runner File _T5Builder.bash
  3. touch task5/000_---------.txt; chmod 000 task5/000_---------.txt ; echo -n "000 "
  4. touch task5/001_--------x.txt; chmod 001 task5/001_--------x.txt ; echo -n "001 "
  5. touch task5/776_rwxrwxrw-.txt; chmod 776 task5/776_rwxrwxrw-.txt ; echo -n "776 "
  6. touch task5/777_rwxrwxrwx.txt; chmod 777 task5/777_rwxrwxrwx.txt ; echo -n "777 "
  7. 000 001 002 003 004 005 006 007 010 011 012 013 014 015 016 017 020 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037 040 041 042 043 044 045 046 047 050 051 052 053 054 055 056 057 060 061 062 063 064 065 066 067 070 071 072 073 074 075 076 ...

前几个文件

  1. ... 761 762 763 764 765 766 767 770 771 772 773 774 775 776 777
  2. Done
  3. total
  4. ---------- 000_---------.txt
  5. ---------x 001_--------x.txt
  6. --------w- 002_-------w-.txt
  7. --------wx 003_-------wx.txt
  8. -------r-- 004_------r--.txt
  9. -------r-x 005_------r-x.txt
  10. -------rw- 006_------rw-.txt
  11. -------rwx 007_------rwx.txt
  12. ------x--- 010_-----x---.txt
  13. ------x--x 011_-----x--x.txt
  14. ------x-w- 012_-----x-w-.txt
展开查看全部

相关问题