Sponsored Content
Full Discussion: Shell basics
Top Forums Shell Programming and Scripting Shell basics Post 302076732 by Perderabo on Thursday 15th of June 2006 10:11:57 AM
Old 06-15-2006
if [ $lines -eq 0 ]

This came first. With the old bourne shell it is all you have. So it works everywhere. But it is not what it seems to be. The if statement runs a command and tests to see if it worked. So you can do stuff like:
if mkdir tmp ; then echo mkdir worked ; fi
mkdir is just a command and tmp is parameter to mkdir. There is a command called [ and like many commands it takes parameters. It checks its last parameter and gets mad unless it is ]. If the last parameter is a ], it looks at everything else and tries to determine what you are trying to test. If you do:
lines=""
if [ $lines -eq 0 ]
you will have a problem. All that the [ command will see is the -eq and the 0 so it will complain. Because of these concerns, Dave Korn invented another syntax. Now you can do:
if [[ $lines = 0 ]]
and it won't get mixed up as easily. Bash has picked this up from ksh and it is a better choice than the single [ most of the time. The idea is that [ might disappear eventually, but I tend to doubt that it will. Still, the official word is that [ is present in ksh to support older scripts and you should switch to [[.

But you asked about "if (($lines = 0 ))". That precise syntax is a terrible mistake and you probably should never use it. First, ksh has built-in arithmetic and you can do stuff like:
((k=7+1)) ; echo $k
and get 8. The ((expression)) syntax is just a command to do arithmetic. This command has an exit code. If the expression is non-zero, the command succeeds. But if the expression is zero, the command fails. So you can test the result of an expression. I often do stuff like:
if ((lines == 0 ))
which tests if lines is equal to 0. With (($lines = 0)), assuming that lines contained the name of another variable, it might almost work:
lines=xyz
if (($lines = 0))
$lines would be replaced by xyz, so the shell would see ((xyz = 0)). xyz would set to 0. ((xyz = 0)) would always fail so the if statement would never be true. You never need to use a $ sign inside ((expression)). This is ok:
((lines=lines+1))
The arithmetic statement will know that lines is a variable and will look up the value. Bash has also picked up arithmetic from ksh.
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basics of shell scripting

Anybody please tell me basics of shell scripts (2 Replies)
Discussion started by: arvind.elle
2 Replies

2. Shell Programming and Scripting

awk basics

what is wrong with the code below; it starts ,then does nothing, (even it doesn't end) #!/bin/awk x=1 b="foo" awk printf("%s got a %d on the last test\n","Jim",83) myout=("%s-%d",b,x) print myout (6 Replies)
Discussion started by: gfhgfnhhn
6 Replies

3. UNIX for Dummies Questions & Answers

Installation basics

hello, Im new to this Os. so, can i get any information'bout installation basics of unix. (1 Reply)
Discussion started by: Abhijit Bhatt
1 Replies

4. Shell Programming and Scripting

SHELL Scripting Basics

Hi, I am new to shell scripting, i have experience in solaris, can anyone share me the link and experience to learn shell scripting from basics Thanks RJS (2 Replies)
Discussion started by: rajasekg
2 Replies

5. Shell Programming and Scripting

LEARN SHELL SCRIPTING BASICS

I am beginner to the SHELL SCRIPT and want to Learn SHELL SCRIPT Basics. This thread should help to all beginners who want to learn SHELL SCRIPT *** Thanks in advance to those who will contribute on this thread *** Please guide me and all beginners... 1. Is there any good e-book... (3 Replies)
Discussion started by: sagarsbhandare
3 Replies

6. UNIX for Dummies Questions & Answers

help me with basics

hello everyone i have to start with unix as it is a part of my training programme and i have to do a self study, i dont know where to start from. i need some basic questions to be answerd like why we use unix ? what is a terminal? what is an editor? why we write commands inside terminal? these... (4 Replies)
Discussion started by: aryancool
4 Replies

7. UNIX for Dummies Questions & Answers

UNIX Basics

Hello, 1) I am trying to get involved in UNIX for educational purposes so I have installed the latest Ubuntu edition 12.04. Do you know another package that I could use it instead for educational purposes? 2)What is the difference between "~$" and "/$" (it comes with cd / and cd ~) .The... (1 Reply)
Discussion started by: Iwn
1 Replies

8. UNIX for Dummies Questions & Answers

UNIX Basics about shell and editors and default settings

Hi all, I have 3-4 years of experience working on unix environment. I am not a beginner, but unix is not my primary skill set. I am very good at awk programming and doing many of my tasks very well, really very weak on basics. I moved to a new job recently and the settings there are driving me... (5 Replies)
Discussion started by: ysvsr1
5 Replies
shift(1)							   User Commands							  shift(1)

NAME
shift - shell built-in function to traverse either a shell's argument list or a list of field-separated words SYNOPSIS
sh shift [n] csh shift [variable] ksh * shift [n] DESCRIPTION
sh The positional parameters from $n+1 ... are renamed $1 ... . If n is not given, it is assumed to be 1. csh The components of argv, or variable, if supplied, are shifted to the left, discarding the first component. It is an error for the variable not to be set or to have a null value. ksh The positional parameters from $n+1 $n+1 ... are renamed $1 ..., default n is 1. The parameter n can be any arithmetic expression that evaluates to a non-negative number less than or equal to $#. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), ksh(1), sh(1), attributes(5) SunOS 5.10 15 Apr 1994 shift(1)
All times are GMT -4. The time now is 08:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy