Best practice for bracket comparisons?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Best practice for bracket comparisons?
# 1  
Old 01-12-2010
Best practice for bracket comparisons?

So, I have no formal higher education in programming at all and am self taught. I am now wondering what would be considered best practices? Like should I hard code a variable, then compare it to what I want to know or achieve, or should I just put the commands with in the brackets?

Example, a script I am writing now loops through /Users to get all local (synchronized) accounts and checks their group membership of the admin group. If they are a member of the admin group a receipt file gets placed on the machine, and the policy is logged as being ran on that machine. If the results return back nothing the script just exits. Then I can build a policy report of every machine in my enterprise that has local admin accounts and go from there.

I know how to code all of this, but I am not sure what would be considered, "best practice."

Like, should I just construct it like this: if [[ some command with a loop == 1 ]] or if [[ $variable == 1 ]] then hard code my variable in the script?

It works either way I suppose.

Thanks for taking the time to read this.

Tom
# 2  
Old 01-12-2010
Several points: [[ and [ may not have precisely the same semantics in some shells. example ksh.
try
Code:
type [  
type [[

Comparisons with string values are [ "string" = "string" ], not ==.

anyway - don't hard code values, except perhaps something that will never change,
example: a status return code of zero always means 'ok', so it can never change.

Why do this? because you can put all of the control variables right at the top of the code, so you can change behavior just by editing a few closely located lines of code. The other reason is that you could miss a "1" somewhere during an edit, completely breaking your code.

A lot of code examples on the forums here do not follow that practice because adding extra variables confuses people just starting out in scripting.
# 3  
Old 01-12-2010
Thanks for your reply, but still got a few questions...

According to this document, which I have used to teach myself shell programming, [[ and [ have different abilities in bash.

Quote:
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
source: Test Constructs

I have also read that = is used for an integer comparison and == is used for a string comparison, but it also says that "=" and "==" are synonyms in that same document. So the double ='s is probably unneeded I take it, but still allowed?

I have been programming in shell for a while now and am just trying to clean up my bad habits, if any to become a more efficient coder. I ended up not hard coding the variable in either, and put the command in the comparison. The script works as I wrote it, but I am getting an odd exit status error when running it in the terminal.

Thanks again for your time.

Tom
# 4  
Old 01-12-2010
Quote:
Originally Posted by tlarkin
Thanks for your reply, but still got a few questions...

According to this document, which I have used to teach myself shell programming, [[ and [ have different abilities in bash.
Yes, BASH has many extensions on the standard shell. But you can't depend on those extensions anywhere else.

As for numerical and string equality being identical, well -- given the same number base and no leading zeroes or extraneous sign symbols, a string comparison is no different than a numerical comparison. I've tried various things to trip them up, things like comparing positive and negative zero... it always acts just like a string comparison whether its one or two equals.
# 5  
Old 01-12-2010
OK, thanks for the input. My script simply checks the local account's membership by grep -c from directory services, and if it returns a 1 then it is in the admin group, if it returns a zero the user is not. Then I I just use if/then to install a dummy receipt in /Library/Receipts from a manual triggered policy. Then I can take that policy log, and have a full list of all assets in the enterprise that have local admin accounts in /Users.

I am using OS X 10.5.8 in case some of this sounds odd to anyone.

Thanks again for the comments. I am sure I will have tons of more questions for you guys.

-T
# 6  
Old 01-12-2010
Quote:
Originally Posted by tlarkin
OK, thanks for the input. My script simply checks the local account's membership by grep -c from directory services, and if it returns a 1 then it is in the admin group, if it returns a zero the user is not. Then I I just use if/then to install a dummy receipt in /Library/Receipts from a manual triggered policy...
Note tha you can use grep without brackets (and need of -c):
Code:
if grep somestring somefile
then
    do something
else
    do something else
fi

for the if/then (without else) statement, you can :
Code:
grep somestring somefile && do something

if there are more than 1 command in the "do something", put it between curly brackets { ... }
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String regex comparisons

Here is the sample code: str1="abccccc" str2="abc?" if ]; then echo "same string" else echo "different string" fi Given that ? implies 0 or 1 match of preceding character, I was expecting the output to be "different string", but I am seeing "same string". Am I not using the... (3 Replies)
Discussion started by: Rameshck
3 Replies

2. Shell Programming and Scripting

File comparisons

Hi all, I want to compare two files based on column value Kindly help me a.txt 123,ABCD 456,DEF 789,SDF b.txt 123,KJI 456,LMN 321,MJK 678,KOL Output file should be like Common on both files c.txt 123,ABCD,KJI (8 Replies)
Discussion started by: aaysa123
8 Replies

3. Shell Programming and Scripting

Pair wise comparisons

Hi, I have 25 groups and I need to perform all possible pairwise compariosns between them using the formula n(n-1)/2. SO in my case it will be 25(25-1)/2 which is equal to 300 comparisons. my 25 groups are FG1 FG2 FG3 FG4 FG5 NT5E CD44 CD44 CD44 AXL ADAM19 CCDC80 L1CAM L1CAM CD44... (1 Reply)
Discussion started by: Diya123
1 Replies

4. UNIX for Dummies Questions & Answers

File and if statement comparisons

I'd love to get help on this one please. Ok so say I have a file called README with lines such as this: index:index.html required:file1.1:file2.1:file3.1 I'm having trouble with writing an if statement that compares the items in a list with a file inside README, what I imagine in my head... (7 Replies)
Discussion started by: mistsong1
7 Replies

5. UNIX for Dummies Questions & Answers

Date comparisons

Hi, I want to perform a simple date comparisons, i.e. select all files modified after a certain date (say 12-feb-2011) I do not have the option of creating a file and using find's -newer option. Any simple way to do this? I can do this by reading the stat command's output and comparing... (10 Replies)
Discussion started by: jawsnnn
10 Replies

6. Shell Programming and Scripting

String comparisons

Can someone please tell me what is wrong with this stings comparison? #!/bin/sh #set -xv set -u VAR=$(ping -c 5 -w 10 google.com | grep icmp_req=5 | awk '{print $6}') echo I like cookies echo $VAR if "$VAR" == 'icmp_req=5' then echo You Rock else echo You Stink fiThis is the error.... (6 Replies)
Discussion started by: cokedude
6 Replies

7. Shell Programming and Scripting

ksh, difference between double bracket and single bracket

Can somebody tell me the difference between double brackets and single brackets, when doing a test. I have always been acustomed to using single brackets and have not encountered any issues to date. Why would somebody use double brackets. Ie if ] vs if Thanks to... (2 Replies)
Discussion started by: BeefStu
2 Replies

8. UNIX for Dummies Questions & Answers

Command comparisons

Hi guys, Im trying to figure out what is the difference between using a | and the command xargs ... examples of usage: 1) ls * | wc -w => this gives you the number of files in the current directory including all subdirectories 2) find . “*.log” | xargs grep ERROR => this gives... (6 Replies)
Discussion started by: avidrunner
6 Replies

9. Shell Programming and Scripting

numeric range comparisons

I have two files.And a sort of matrix analysis. Both files have a string followed by two numbers: File 1: A 2 7 B 3 11 C 5 10 ...... File 2: X 1 10 Y 3 5 Z 5 9 What I'd like to do is for each set of numbers in the second file indicate if the first or second number (or both) in... (7 Replies)
Discussion started by: dcfargo
7 Replies

10. UNIX for Dummies Questions & Answers

Can grep do numerical comparisons?

Say for example I have a list of numbers.. 5 10 13 48 1 could I use grep to show only those numbers that are above 10? For various reasons I can only use grep... not awk or sed etc. (7 Replies)
Discussion started by: Uss_Defiant
7 Replies
Login or Register to Ask a Question