I need to check the file permissions of directories /usr, /tmp, /var and their contents of sub directories. I have taken the file permissions of the directory and its sub directory files in an array separately for each of /usr, /tmp, and /var:

fun() {
  set -A  PR_Uperm -- "/usr" `ls -lRt /bin|grep -v "total"|sed -e '1d' -e '/^$/d' -e '/^l/d'|awk '{print $1}'`
  set -A  PR_Tperm -- "/tmp" `ls -lRt /bin|grep -v "total"|sed -e '1d' -e '/^$/d' -e '/^l/d'|awk '{print $1}'`

Finally I want these separate arrays to be in single array which is in a different function, as we have dynamic scope in scripts. I have tried as below:

  set -A perm ${PR_Uperm[@]} ${PR_Tperm[@]}

when I execute the script I am getting this error.

script.sh:79919: subscript out of range

when I give only one array to perm array as below,I am getting no error.

set -A perm ${PR_Tperm[@]}

I have so many entries to store in perm array. How can I increase size of array dynamically? set -A is not related to bash, but this dynamic increasing might be same for bash and ksh, so I have included it.

I have tried to store in a temporary file as shown below:

printf "/usr\n`ls -lRt /usr|grep -v "total"|sed -e '1d' -e '/^$/d' -e '/^l/d'|awk '{print $1}'`\n" >> /tmp/output2.txt  # not working in ksh, but working when I run as bash
ksh: no space

ls -lRt /usr|grep -v "total"|sed -e '1d' -e '/^$/d' -e '/^l/d'|awk '{print $1}' >> /tmp/output2.txt # it is working

Why did the first command run successfully in bash but in ksh ?

  • Have you consider usage of temporary file instead of array? – Romeo Ninov Apr 26 '17 at 6:25
  • @Romeo ninov I have tried that way, It saying no space. test.sh[20]: no space – Sriram P Apr 26 '17 at 6:32
  • Place this temporary file in filesystem where you have enough diskspace – Romeo Ninov Apr 26 '17 at 6:47
  • 1
    1) Please don't parse ls, that is very likely to fail. 2) It seems very, very unlikely that you actually need to have all of this stored in an array. Why don't you just iterate over the list dynamically in a loop? Why do you need to have everything saved first? Maybe if you explain what your script is trying to do we can give you a simpler approach. 3) If you find yourself setting up an array with thousands of elements, it is a sign that you should switch to a more powerful language. The shell is very limited and not a "real" scripting language. – terdon Apr 26 '17 at 8:47

Bash has, if I recall correctly, no built-in limit on array size. KornShell93 supports indexes up to about 64k in recent releases (and up to 4095 in older releases).

There is a utility called mtree which is used to compare a file hierarchy with a specification. Any deviation from the specification (missing files or directories, or ownership/permission discrepancies) are reported and optionally corrected (if possible).

Since I don't know what Unix you're on, I can't further point you to where to find this utility. It's in OpenBSD's base system, but is evidently also available for Linux.

  • I am working on KSH, I didn't get why it is throwing out of range error, and when I am trying to store in temporary file, It throwing me no space error. – Sriram P Apr 26 '17 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.