Problems understanding example code

I am really new to UNIX and programming in general and so apologies if this thread is a bit simple.

I have searched and found a piece of sample code for a training program I am currently undertaking, but seeing as I am relatively new, I dont completely understand how it works.

Here is the code:

file=$1
set -- $(ls -ld $file)

perms=$1
owner=$3

[[ "$perms" = ?r???????? ]] && owner_read=YES
[[ "$perms" = ??w??????? ]] && owner_write=YES
[[ "$perms" = ???x?????? ]] && owner_exec=YES

[[ "$perms" = ????r????? ]] && group_read=YES
[[ "$perms" = ?????w???? ]] && group_write=YES
[[ "$perms" = ??????x??? ]] && group_exec=YES

[[ "$perms" = ???????r?? ]] && world_read=YES
[[ "$perms" = ????????w? ]] && world_write=YES
[[ "$perms" = ?????????x ]] && world_exec=YES

echo "perms: $perms"
echo "OWNER $owner ${owner_read:-NO} ${owner_write:-NO} ${owner_exec:-NO}"
echo "GROUP ${group_read:-NO} ${group_write:-NO} ${group_exec:-NO}"
echo "WORLD ${world_read:-NO} ${world_write:-NO} ${world_exec:-NO}"

Basically I understand how the script works generally, but I am just wondering whether the [[ ]] is equal to a shorthand if statement?

Also is ${owner_read:-NO} checking if the result from the above statement is null and then if so printing "NO"?

Sorry if this is a bit simple.

Thanks for any help.

Hi.

Yes [[ ]] is essentially a short-hand for an if statement in this case.

${owner_read:-NO} will return NO if owner_read is not set or is empty.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Just a couple more quick questions.

set -- $(ls -ld $file)

What is the use of the double --?

I have read up and it states that it doesnt allow change of any of the flag, useful in setting $1 to "-".

Im not 100% sure what it means, I have tested with one - and two and found that you could not change the file name whilst using one as an error occured.

Hi.

(I'm not sure if this is the technically accurate meaning, but...) set -- is used to clear or set positional arguments.

i.e.

> cat Test
set -- $(ls -l Test)
echo "\"$1\""
echo "\"$9\""
echo

set --
echo "\"$1\""
echo "\"$9\""


> ./Test a b c
"-rwx------"
"Test"

""
""

set -- will remove or overwrite args that you pass to the script (i.e. a b c in this case)

-- is commonly used by programs to signal that there are no more arguments to follow.

Thank you. Was just what I needed to know!

It indicates the end of option arguments; any further arguments will not be parsed as options. This is true of -- with any command.