Advertisement
AlvinSeville7cf

Format permissions

Sep 6th, 2021
2,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 1.05 KB | None | 0 0
  1. function formatPermissions(permissionString) {
  2.   if (permissionString !~ /^[r-][w-][x-]$/)
  3.     return "Error: wrong persmission specified"
  4.  
  5.   result = ""
  6.  
  7.   if (substr(permissionString, 1, 1) == "r")
  8.     result = result "readable "
  9.   if (substr(permissionString, 2, 1) == "w")
  10.     result = result "writable "
  11.   if (substr(permissionString, 3, 1) == "x")
  12.     result = result "executable"
  13.  
  14.   gsub(/\s$/, "", result)
  15.   gsub(/\s/, " and ", result)
  16.   return result
  17. }
  18.  
  19. function formatAllPermissions(permissionString) {
  20.   if (permissionString !~ /^([r-][w-][x-]){3}$/)
  21.     return "Error: wrong persmission specified"
  22.  
  23.   ownerPermissions = formatPermissions(substr(permissionString, 1, 3))
  24.   groupPermissions = formatPermissions(substr(permissionString, 4, 3))
  25.   otherPermissions = formatPermissions(substr(permissionString, 7, 3))
  26.  
  27.   result = "Owner: " ownerPermissions " | Group: " groupPermissions " | Other: " otherPermissions
  28.   return result
  29. }
  30.  
  31. NR > 1 {
  32.   permissions = substr($1, 2)
  33.   print formatAllPermissions(permissions), "=>", $9
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement