D:\JavaFrameworks\InsuranceFramework\src\java\unix_code\SHELL_PROGRAMMING.properties |
1 SHELL VARIABLES
2 Variable name must start with a letter or underscore.
3 Example:
4
5 joe i6 my_last_entry _joe
6
7 The shell variables have NO TYPE and ASSIGNMENT is treated as a string of characters:
8
9 message="I am the Message"
10
11 joe=mike
12
13 When you assign a variable that has embedded blanks or commas use double quotes around the string.
14
15 DISPLAYING THE VALUE OF A SHELL VARIABLE:
16
17 To assign a value to variable, you must set a value to it.
18
19 $ length=80
20
21 $ message="********** joe ************"
22
23 To access a variable or display its value you must precede the variable name with $
24
25 For example:
26
27 $ echo $length
28 80
29 $
30
31 $ echo $message
32 **************** joe ***********
33 $
34
35 If the variable has no value an empty string is displayed
36
37 $ length=80
38 $ echo the length is $length
39 the length is 80
40 $
41
42 $ echo $no_value # nothing is displayed
43
44 $ echo :: $no_value
45 ::
46 $
47 ========================================================
48 ========================================================
49 USING SHELL VARIABLE
50
51 1) Assign Value to Variable:
52
53 $ my_file="my_program.c"
54 $ wc l $my_file
55 115 my_program.c
56
57 2) Copy from another variable:
58
59 $ my_file="my_program.c"
60 $ his_file=$my_file
61 $ echo his_file
62 my_program.c
63
64 3) Copy command or pipe into a variable:
65
66 $ option=" l"
67 $ my_file="my_program.c"
68 $ command1="wc $option $myfile"
69 $ command1
70 115 my_program.c
71
72 4) Copy a PATH into a variable:
73
74 $ cd /usr/usr2/faculty/noureld/sample5
75 $ my_path="/usr/usr2/faculty/noureld/sample5"
76 $ cd my_path
77 $
78 ========================================================
79 ========================================================
80 FOR STATEMENT
81
82 UNIX shell provides a FOR LOOP
83
84 for variable in list
85 do
86 command
87 . . .
88 done
89
90 NOTE:
91 The variable in the for loop has no $ in loop control variable.
92 The variable within the body of the loop has $ before it
93
94 Example:
95
96 for count in 1 2 3 4 5 6 7 8 9
97 do
98 echo $count
99 done
100
101
102 To write this loop on the system:
103
104 $ for count in 1 2 3 4 5 6 7 8 9
105 > do
106 > echo $count
107 > done
108
109 1
110 2
111 ....
112 9
113 $
114 Example2:
115
116 $ for my_file in *.c
117 > do
118 > echo "********************************"
119 > echo " file $my_file is sorted"
120 > sort $my_file
121 > echo "********************************"
122 > echo
123 > done
124 $
125 ========================================================
126 ========================================================
127 ALL ABOUT QUOTES
128
129 There are four quote characters in shell programming and each has it own operation.
130
131 1) "" DOUBLE QUOTES
132 2) '' SINGLE QUOTES
133 3) \ BACKSLASH
134 4) `` SINGLE BACK QUOTES - ACCENT
135
136 DOUBLE QUOTES:
137
138 Double Quotes present a string constant. It teats commands as a string
139
140 constant with some exceptions:
141
142 $ echo *
143 file_one
144 file_two
145
146 $ echo "*"
147 *
148
149 $ echo > # append an output
150 syntax error:.....
151
152 $ echo ">"
153 >
154 $
155
156 Exception to the Rules are:
157
158 $ " ' ` \
159
160
161 $ my_file="my_prog.c"
162 $ echo $my_file or $ echo "$my_file"
163 my_prog.c
164 $
165
166 NOTE also "" are used as a list
167
168 $ grep "var_one, var_2, var_3" my_prog.c
169
170 SINGLE QUOTES:
171
172
173 It treats the string '....' as absolute constant string with no evaluation to the string:
174
175
176 $ msg="hello" $ msg="hello"
177 $ echo "$msg" $ echo '$msg'
178 hello $msg
179 $ $
180
181 ========================================================
182 ========================================================
183 BACKSLASH:
184
185 Backslash negate the special characters to only be presented as characters.
186
187 $ my_file="program.c"
188 $ echo \$my_file
189 $ my_file
190
191
192
193 $ echo \> \< \* \` \' \" \$ \? \\
194 > < * ` ' " $ ? \
195
196 BACK QUOTES:
197
198 Backquotes gives the flexibility to insert commands within a string. These commands are executed first, then their output is inserted back to string in the commands place in the backquoted string.
199
200 $ echo "the date of the day is `date`"
201
202
203 First the `date` command is executed and the result is inserted in the string then echo command is executed.
204
205 {echo "the date of the day is SUN MAR 13 : 22: 33 EAST 1991"}
206
207 the date of the day is SUN MAR 13 : 22: 33 EAST 1991
208
209
210 $ echo your current working directory is `pwd`
211 your current working directory is /usr/faculty/noureld/
212
213
214 You can pipe within the backquoted string.
215
216 $ echo there are `who | wc l` users logged in
217 there are 5 user logged in
218
219 $ now=`date`
220 $ echo $now
221 SUN MAR 13 : 22: 33 EAST 1991
222
223 $ my_list=`cat name*`
224 $ echo $my_list
225 ....
226
227 NEW LINE TAB CONTINUE:
228
229 Similar to printf in C language the echo command can use to display new line, tabs and continue on the same line.
230
231 $ echo "one \n two \n\n three \n\n\n four"
232 one
233 two # followed by two lines
234
235
236 three # followed by three lines
237
238
239
240 four
241
242
243 $ echo "one \t two \t\t three \t\t\t four"
244 one two three four
245
246
247 $ echo "continue on the same line\c"
248 continue on the same line $
249
250
251
252 Example:
253
254 $ for count in 1 2 3 4 5 6
255 > do
256 > echo "$count\c"
257 > done
258 1 2 3 4 5 6
259
260 SHELL ARGUMENTS:
261
262 Shell programs can take arguments as parameters passing to that shell program.
263
264 $ shl_prog_name argument_1 arg2 arg3 arg4
265
266 The position of the argument assigns a variable name to this argument.
267
268 argument_1 is assigned to the shell variable $0
269 arg2 is assigned to the shell variable $1
270 arg3 is assigned to the shell variable $2
271 arg4 is assigned to the shell variable $3
272
273 These shell variables can be used within your shell program.
274
275 ========================================================
276 ========================================================
277 $# and $ * Variables:
278
279 The shell has two variables that are automatically set whenever you execute the shell program. These variable are $# and $*
280
281 $#
282
283 This variable has the number of arguments that are passed to the shell program.
284
285 $*
286
287 This variable has a string that contains the all the passed arguments as one string.
288
289 $ shl_prog_name argument_1 arg2 arg3 arg4
290
291 For the above shell program the two variables are
292 $# = 4 # four arguments
293 $* = "shl_prog_name argument_1 arg2 arg3 arg4" string.
294
295
296
297 if [ “$#” -gt 2 ]
298 then
299 echo "error: Too many arguments. Must provide two arguments."
300 echo "usage: $0 search-pattern file"
301 exit
302 elif [ “$#” -lt 2 ]
303 then
304 echo "error: Not enough arguments. Must provide two arguments."
305 echo "usage: $0 search-pattern file"
306 exit
307 elif [ -f $2 ] # Verify that the command line argument is a file
308 then
309
310
311 /export/home/zero3/srchfile.sh $1 $2
312 elif [ ! -f $2 ] # Verify that the command line argument is a file
313 then
314 echo "error: Second argument must be a file"
315 echo "usage: $0 search-pattern file"
316 exit
317 fi
318
319
320 "<" - Redirect
321 we could redirect the input to grep(which searches for strings within files) so that it comes from a file like this:
322
323 grep searchterm < file
324
325 ">>" Redirect the standard output to file instead of the screen:
326 We use >> to append stdout to a file, for instance, if we wanted to append the date to the end of a file we could redirect the output from date like so:
327
328 date >> file
329
330 "2>" Redirect the standard error to a file:
331 One can redirect standard error (stderr) to a file by using 2>, if we wanted to redirect the standard error from commandA to a file we would use:
332
333 commmandA 2>
334
335 ===========================================================
336 IF STATEMENT:
337
338 UNIX shell provide branch statement or IF statement.
339 if condition
340 then
341 command
342 . . .
343 fi # end of the if statement
344
345
346 The condition is a test expression.
347
348 $if [ $user = "steve" ]
349 > then
350 > echo "hello, steve"
351 > fi
352
353 ========================================================
354 ========================================================
355 RELATION OPERATOR:
356 For string:
357 = test for two string if they are equal
358 "$user" = "steve"
359
360
361 "$user" != "joe"
362
363 n a string that has nonzero length
364 n "$my_file"
365
366 z a string that has a zero length
367 z "$name"
368 INTEGER:
369
370 eq equality
371
372 -ne not equal
373
374 lt less than
375
376 le less than or equal to
377
378 gt greater than or equal to
379
380 ge greater than or equal to
381
382
383 AND and OR OPERATORS:
384
385 a AND
386
387 o OR
388
389
390 EXAMPLE:
391
392 if [ $count gt 5 a $count le 100]
393
394
395 ELSE & ELIF:
396
397 Shell programming provides else and elif to if statement.
398
399 if condition
400 then
401 command
402 . . .
403 else
404 command
405 . . .
406 fi
407
408 EXAMPLE 1:
409 hours = `date | cut c 12 13`
410 if [ "$hours" ge 0 a "$hours" lt 12 ]
411 then
412 echo "GOOD MORNING"
413 else
414 echo " GOOD DAY"
415 fi
416
417 EXAMPLE 2:
418 hours = `date | cut c 12 13`
419 if [ "$hours" ge 0 a "$hours" lt 12 ]
420 then
421 echo "GOOD MORNING"
422 elif [ "$hours" ge 12 a "$hours" lt 18 ]
423 echo " GOOD AFTERNOON"
424 else
425 echo "GOOD EVENING"
426 fi
427 ARITHMETIC OPERATIONS WITH SHELL VARIABLES:
428
429 the shell evaluates every input as string constant. so
430 $ count = 1 + 2
431 $ echo "$count"
432 1 + 2
433 $
434
435 In order to evaluate the math expression, you must use the (function) program EXPR
436
437 $ expr 1 + 5
438 6
439
440 NOTE:
441 the blanks are necessary because the input is looking for an input made up of strings separated by blanks.
442
443 1+2 is one string but 1 + 2 are three strings
444
445 $ expr " 5 * 10" # " 5 * 10" is one input string
446 $ sum = 100
447 $ expr $sum "*" 3
448 300
449
450 To prevent the echo on the screen, you can use backquotes to assign a command to a variable.
451
452 $ result = `expr 1 + 5`
453 $ echo $result
454 6
455
456 NOTE:
457 Korn shell does provide a built in integer arithmetic operators.
458
459 WHILE STATEMENT:
460
461 Shell while statement is typical to any other programming language.
462
463 while condition
464 do
465 command
466 . . .
467 done
468
469 EXAMPLE:
470
471 count=1
472 while [ "$count" le 10]
473 do
474 echo $count
475 count=`expr $count + 1`
476 done
477
478 OUTPUT:
479
480 1
481 2
482 ....
483 10
484
485 READ FROM TERMINAL:
486
487 To get an input from the key board you use the read command.
488
489 $ read user_input
490
491 $ echo $user_input
492
493 You can read more than one entry per line
494
495 $ read one two three
496
497
498 The input string must have white space as a separators.
499
500 CASE STATEMENT:
501
502 Case statement has the following syntax:
503
504 case $variable_name in
505 constant_1 ) statement;;
506 constant_2 ) statement_1 # 0 or more statements
507 statement_2
508 statement_3;; # ";;" terminates the statement
509 * ) statement;; # default case
510 esac # end of case statement
511
512
513 Example:
514
515 read choice_2
516 case "$choice_2"
517 in
518 1 ) chmod "$permission""+r" $tfile_name;;
519 1 ) chmod "$permission"" r" $tfile_name
520 echo "\n this is the reverse of choice 1"
521 echo "\n can not find anything to do";;
522 2 ) chmod "$permission""+w" $tfile_name;;
523 2 ) chmod "$permission"" w" $tfile_name;;
524 3 ) chmod "$permission""+x" $tfile_name;;
525 3 ) chmod "$permission"" x" $tfile_name;;
526 * ) echo "\n no choice is picked";;
527 esac
528
529 ========================================================
530 ========================================================
531
532 By default a normal command accepts input from standard input, which we abbreviate to stdin, standard input is the command line in the form of arguments passed to the command. By default a normal command directs its output to standard output, which we abbreviate to stdout, standard output is usually the console display. For some commands this may be the desired action but other times we may wish to get our input for a command from somewhere other than stdin and direct our output to somewhere other than stdout. This is done by redirection:
533
534 We use > to redirect stdout to a file, for instance, if we wanted to redirect a directory listing generated by the ls we could do the following:
535
536 ls > file
537
538 We use < to specify that we want the command immediately before the redirection symbol to get its input from the source specified immediately after the symbol, for instance, we could redirect the input to grep(which searches for strings within files) so that it comes from a file like this:
539
540 grep searchterm < file
541
542 We use >> to append stdout to a file, for instance, if we wanted to append the date to the end of a file we could redirect the output from date like so:
543
544 date >> file
545
546 One can redirect standard error (stderr) to a file by using 2>, if we wanted to redirect the standard error from commandA to a file we would use:
547
548 commmandA 2>
549
550 Pipelines are another form of redirection that are used to chain commands so that powerful composite commands can be constructed, the pipe symbol '|' takes the stdout from the command preceding it and redirects it to the command following it:
551
552 ls -l | grep searchword | sort -r
553
554
555 The example above firsts requests a long (-l directory listing of the current directory using the ls command, the output from this is then piped to grep which filters out all the listings containing the searchword and then finally pipes this through to sort which then sorts the output in reverse (-r, sort then passes the output on normally to stdout.
556
557
558