Problems understanding example code


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problems understanding example code
# 1  
Old 07-29-2009
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:

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.

Last edited by DukeNuke2; 07-29-2009 at 06:51 AM..
# 2  
Old 07-29-2009
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.
# 3  
Old 07-29-2009
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 [code] and [/code] 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
# 4  
Old 07-29-2009
Just a couple more quick questions.

Code:
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.
# 5  
Old 07-29-2009
Hi.

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

i.e.
Code:
> 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.
# 6  
Old 07-29-2009
Thank you. Was just what I needed to know!
# 7  
Old 07-29-2009
Quote:
Originally Posted by Makaer
Just a couple more quick questions.

Code:
set -- $(ls -ld $file)

What is the use of the double --?

It indicates the end of option arguments; any further arguments will not be parsed as options. This is true of -- with any command.
Quote:
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Understanding Assembly Code

As the title suggests, I want to better understand the following assembly code: section .text global main ; must be declared for linker (gcc) main: ; tell linker entry point mov edx, len ; message length mov ecx, msg ; message to write... (2 Replies)
Discussion started by: Azrael
2 Replies

2. UNIX for Dummies Questions & Answers

Understanding bash code

I am not able to understand below line in unix bash shell.Could anyone explain what it will do result="${path1}/*${var1}*${var2}*wssreligibleitem*.csv" path1 is defined and it is a directory path var1 is defined and it holds string value like abc var2 is defined and it holds string value like... (6 Replies)
Discussion started by: vamsi.valiveti
6 Replies

3. Shell Programming and Scripting

Help with perl code understanding

Hi, I need to understand below perl code, can some one advise me. perl -MDate::Parse -e'BEGIN{$main::now=time;$main::old=(time-60*30)}' -nE'if(/^(\w+\s+\d+\s+\d+:\d+:\d+)/) {$t=str2time $1; $t > $old && $t < $now && print}' (1 Reply)
Discussion started by: learnbash
1 Replies

4. Programming

Understanding perl code

What is the difference between the two statements below? A: $a->{"$fruit"}->{"$color"}->{size} = $size B: $size = $a->{"$fruit"}->{"$color"}->{size} Please assist. Thanks! (0 Replies)
Discussion started by: onlinelearner02
0 Replies

5. Shell Programming and Scripting

Help understanding some Perl code.

Well, I found myself trying to fix some Perl code (Ive never done any Perl in my life) and I pinpointed the place where the bug could be. But to be sure I have to know what does a few line of code mean: $files_lim =~ (/^\d*$/) $files_lim =~ (/^\d*h$/) $files_age =~ s/h// The code where... (2 Replies)
Discussion started by: RedSpyder
2 Replies

6. Shell Programming and Scripting

Help understanding Perl code.

Well, I found myself trying to fix some Perl code (Ive never done any Perl in my life) and I pinpointed the place where the bug could be. But to be sure I have to know what does a few line of code mean: $files_lim =~ (/^\d*$/) $files_lim =~ (/^\d*h$/)$files_age =~ s/h//The code where this was... (0 Replies)
Discussion started by: RedSpyder
0 Replies

7. Programming

Problems understanding pipes

I am trying to create a csh clone, but am having problems implementing piped commands. Specifically, the below code simply hangs after input of ls | grep <text> It does however filter the output and display it correctly, but it appears that grep hasn't exited and my shell never comes back to the... (16 Replies)
Discussion started by: ab_tall
16 Replies

8. UNIX Desktop Questions & Answers

Understanding the code

hello all, May i know what is this "DEBUG_ME $DEBUG_CMD main" doing in the below code. I am confused with alias also "alias DEBUG_ME='#'". Thanks for your help. set -x alias DEBUG_ME='#' if ; then . /product/apps/informatica/v7/pc/ExtProc/debug.ksh "$1" fi # Declaring the... (1 Reply)
Discussion started by: Ariean
1 Replies

9. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: Makaer
1 Replies

10. UNIX for Dummies Questions & Answers

Understanding Code in IF LOOP

Hello All, I would like to know and understand the difference between the below 3 IF loops and also if possible what are the different other parameters i could use other than those mentioed in the below lF LOOP conditions, appreciate your help. Thanks, Sam. (1 Reply)
Discussion started by: Ariean
1 Replies
Login or Register to Ask a Question