You may already be familiar with some redirection, such as. which saves the output of the ls command into a file called lsoutput.txt.
|
|
- Stuart Ward
- 5 years ago
- Views:
Transcription
1 Shell Programming A shell is a program that acts as the interface between you and the Linux system, enabling you to enter commands for the operating system to execute. In that respect, it resembles the Windows command prompt, but much more powerful You may already be familiar with some redirection, such as $ ls -l > lsoutput.txt which saves the output of the ls command into a file called lsoutput.txt. One reason to use the shell for programming is that you can program the shell quickly and simply. On Linux, the standard shell that is always installed as /bin/sh is called bash You can check the version of bash you have with the following command: Output $ /bin/bash version husaingholoom$ /bin/bash --version GNU bash, version (1)-release (x86_64-apple-darwin11) Copyright (C) 2007 Free Software Foundation, Inc. Fall 2013 CS4350 Husain Gholoom Page 1
2 Objective : What is shell (interpreted vs. complied) Variables Conditions Flow structures What is shell A Unix program executed when you log in A command interpreter provides the basic user interface to UNIX utilities A programming language program consisting of shell commands is called a shell script put commands in a file and execute it make the file executable (chmod u+x script-file ) Compiled vs. Interpreted Com piler g++/gcc Executable Fall 2013 CS4350 Husain Gholoom Page 2
3 Examples : Compiled Languages : C / C++, FORTRAN, Pascal, Cobol Interpreted Languages : Unix shell, scripts, Perl, Basic (in 80's) Shells Bourne shell Stephen Bourne at Bell Labs, 1977 C shell Bill Joy at Berkeley (Sun) More C-like interface Ken Thompson s shell Bourne sh - basic Bourne shell bsh - another basic Bourne shell bash - GNU Bourne Again shell C Shell csh - basic C shell tcsh - extended C shell What s the difference? Internal execution Basic commands (ls, cd) are same Which one am I in? echo $SHELL you will get Type exit to exit the shell /bin/bash Fall 2013 CS4350 Husain Gholoom Page 3
4 Language Components : A programming language has Variables Arrays Flow Controls If-then-else - AND - OR while do for do until do switch Classes (Unix doesn t have these) Why Use Shell? Everything can be done in C/C++ Shell scripts Really, really fast to write Use once, throw away Powerful library functions Example: show the file list in a directory and find a file with a name ABC First Shell Script Create a file hello #!/bin/sh #This is an example e echo Hello World exit 0 Execute the file chmod +x hello./hello Fall 2013 CS4350 Husain Gholoom Page 4
5 First Shell Script Lines starting with # are comments. The first line of your script file begin with: #!pathname optional arguments pathname is an absolute pathname of the interpreter (/bin/sh, or /bin/bash). The last line returns the status of the script to the system. exit 0 (successful) exit 1 (fail) Local Variables Set value var=value value is a string, even in the format of a number On the command line, you can see this in action when you set and check various values of the variable salutation: Example $ salutation=hello $ echo $salutation Hello $ salutation= Yes Dear $ echo $salutation Yes Dear $ salutation=7+5 $ echo $salutation 7+5 Fall 2013 CS4350 Husain Gholoom Page 5
6 Retrieve value Quote $var : the value ${var} : the value $var : the value as a string $var : the name \ preserves the literal value of the next character (\$1) literal value of enclosed characters ( \$1 ) " literal value of enclosed characters except: $ retains its special meaning and \ retains its special meaning when followed by $ " \ l myvar=val echo myvar echo $myvar echo myvar echo $myvar echo \$myvar echo \$$myvar echo \ $myvar\ echo \ $myvar\ myvar val myvar val $myvar $val "val" 'val' Fall 2013 CS4350 Husain Gholoom Page 6
7 Example : Create a file and type in the following #!/bin/sh myvar= Hi there echo $myvar echo $myvar echo $myvar echo \$myvar echo Enter some text read myvar echo $myvar now equals $myvar exit 0 Exit the editor Save the file Change mode ( chmod +x filename.sh ) Execute the shell (./filename.sh ) The Result is $./variable Hi there Hi there $myvar $myvar Enter some text Hello World $myvar now equals Hello World Fall 2013 CS4350 Husain Gholoom Page 7
8 Environment Variable $HOME : The home directory of the user $PATH : A colon-separated list of command directories $PS1 : The command prompt $IFS : The input string separator This is a list of characters that are used to separate words when the shell is reading input, usually space, tab, and newline characters. $SHELL : The shell interpreter Use set or env to show the environment variables Parameter Variable : If your script is invoked with parameters, some additional variables are created. If no parameters are passed, the environment variable $# still exists but has a value of 0. $? : the exit status of last process $0 : the name of the shell script $# : the number of parameters $$ : the process ID of the shell script $1,$2,... : the parameters $* : the list of all parameters, by $IFS $@ : the list of all parameters, by Fall 2013 CS4350 Husain Gholoom Page 8
9 Example : Try the following script #!/bin/sh salutation="hello" echo $salutation echo "The program $0 is now running" echo "The second parameter was $2" echo "The first parameter was $1" echo "The parameter list was $*" echo "The user's home directory is $HOME" echo "Please enter a new greeting" read salutation echo $salutation echo "The script is now complete" exit 0 If you run this script, you get the following result: husain-gholooms-macbook:shell husaingholoom$./sc5.sh Texas State University Hello The program./sc5.sh is now running The second parameter was State The first parameter was Texas The parameter list was Texas State University The user's home directory is /Users/husaingholoom Please enter a new greeting CS4350 Unix Programming CS4350 Unix Programming The script is now complete husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 9
10 Variable Shell script mytest IFS= ; echo $* echo $@ echo $0 Command./mytest arg1 arg2 arg3 Result arg1;arg2;arg3 arg1 arg2 arg3./mytest Condition Test if test condition if [ condition ] then then statements statements fi fi if test condition ; then statements fi if [ condition ]; then statements fi if [ condition ] then statements elif [ condition ] then statements else statements fi if [ condition ]; then statements elif [ condition ]; then statements else statements fi Fall 2013 CS4350 Husain Gholoom Page 10
11 Control and Condition A common use for if is to ask a question and then make a decision based on the answer: For Example : Script mytest #!/bin/sh echo "Is it morning? Please answer yes or n" read timeofday if [ $timeofday = "yes" ]; then echo "Good morning" else echo "Good afternoon" fi exit 0 Run the script by typing the following Command husain-gholooms-macbook:shell husaingholoom$./mytest.sh The Result is Is it morning? Please answer yes or n yes Good morning husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 11
12 Condition String comparison string1 = string2 string1!= string2 -n string -z string True if The strings are equal The strings are not equal The string length is not zero The string length is zero (NULL or Empty String ) Arithmetic Expression expr1 -eq expr2 expr1 -ne expr2 expr1 -gt expr2 expr1 -ge expr2 expr1 -lt expr2 expr1 -le expr2!expr File condition -e file -d file -f file -r file -w file -x file -s file True if equal not equal greater than greater than or equal to less than less than or equal to false Treu if exist directory regular file readable writable executable non-zero size Fall 2013 CS4350 Husain Gholoom Page 12
13 Example : Script mytest #!/bin/sh if [ -f /bin/bash ] then echo "file /bin/bash exists" fi if [ -d /bin/bash ] then echo "/bin/bash is a directory" else echo "/bin/bash is NOT a directory" fi Run the script by typing the following Command husain-gholooms-macbook:shell husaingholoom$./mytest.sh The Result is file /bin/bash exists /bin/bash is NOT a directory husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 13
14 for variable in values do statements done Script mytest For Loop #!/bin/sh for file in $(ls *.*) ; do echo "This is $file" done Run the Script by using the following Command./mytest The Result is This is sc1.sh This is sc2.sh This is sc3.sh This is sc4.sh husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 14
15 When condition is true, loop while condition do statements done While Loop Script sc7.sh #!/bin/sh echo "Enter password" read trythis while [ "$trythis"!= "secret" ]; do echo "Sorry, try again" read trythis done exit 0 After you change the mode, Use the following command to run the script The Result is husain-gholooms-macbook:shell husaingholoom$./sc7.sh Enter password secrete Sorry, try again secret husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 15
16 Until Loop When condition is not true, loop until condition do statements done Script sc8.sh #!/bin/sh COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done After you change the mode, Use the following command to run the script The Result is husain-gholooms-macbook:shell husaingholoom$./sc8.sh COUNTER 20 COUNTER 19 COUNTER 18 COUNTER 17 COUNTER husain-gholooms-macbook:shell husaingholoom$ vi sc8.sh Fall 2013 CS4350 Husain Gholoom Page 16
17 Case case variable in pattern1 ) statements ;; pattern2 ) statements ;;... esac Script sc9.sh #!/bin/sh # An example with the case statement # Reads a command from the user and processes it echo "Enter your command (who, list, or cal)" read command case "$command" in who) echo "Running who..." who ;; list) echo "Running ls..." ls ;; cal) echo "Running cal..." cal ;; *) echo "Bad command, your choices are: who, list, or cal" ;; esac exit 0 Fall 2013 CS4350 Husain Gholoom Page 17
18 After you change the mode, Use the following command to run the script husain-gholooms-macbook:shell husaingholoom$./sc9.sh The Result is husain-gholooms-macbook:shell husaingholoom$./sc9.sh Enter your command (who, list, or cal) who Running who... husaingholoom console Sep 7 08:09 husaingholoom ttys000 Sep 7 09:48 husain-gholooms-macbook:shell husaingholoom$./sc9.sh Enter your command (who, list, or cal) list Running ls... sc1.sh sc10.sh sc2.sh sc3.sh sc4.sh sc5.sh sc6.sh sc7.sh sc8.sh sc9.sh husain-gholooms-macbook:shell husaingholoom$./sc9.sh Enter your command (who, list, or cal) cal Running cal... September 2013 Su Mo Tu We Th Fr Sa husain-gholooms-macbook:shell husaingholoom$./sc9.sh Enter your command (who, list, or cal) what Bad command, your choices are: who, list, or cal husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 18
19 Break and Continue break escape from the current for, while, or until loop continue continue at the next iteration of the current for, while, or until loop Script sc10.sh #!/bin/bash x=0 while [ $x -le 5 ] do echo "Before break : $x" x=`expr $x + 1` break echo "After breakn : $x" done echo "While loop finished" After you change the mode, Use the following command to run the script husain-gholooms-macbook:shell husaingholoom$./sc10.sh The Result is Before break : 0 While loop finished husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 19
20 Script sc11.sh #!/bin/bash x=0 while [ $x -le 5 ] do echo "Before continue : $x" x=`expr $x + 1` continue echo "After continue : $x" done echo "While loop finished" After you change the mode, Use the following command to run the script husain-gholooms-macbook:shell husaingholoom$./sc11.sh The Result is Before continue : 0 Before continue : 1 Before continue : 2 Before continue : 3 Before continue : 4 Before continue : 5 While loop finished husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 20
21 The AND list The AND list construct enables you to execute a series of commands, executing the next command only if all the previous commands have succeeded. The syntax is : statement1 && statement2 && statement3 &&... What is the output of the following script #!/bin/sh touch file_one touch file_two if [ -f file_one ] && echo "hello" && [ -f file_two ] && echo " there" then echo "in if" else echo "in else" fi exit 0 What happens if you change the line that contains touch file_two to be rm f file_tw and run the script again? Try the script and you ll get the following result: hello there in if Fall 2013 CS4350 Husain Gholoom Page 21
22 The OR List The OR list construct enables us to execute a series of commands until one succeeds, and then not exe- cute any more. The syntax is as follows: statement1 statement2 statement3... What is the output of the following script #!/bin/sh rm -f file_one if [ -f file_one ] echo "hello" echo " there" then echo "in if" else echo "in else" fi exit 0 This results in the following output: hello in if Fall 2013 CS4350 Husain Gholoom Page 22
23 expr Command Purpose Evaluates arguments as expressions. Syntax expr Expression Description The expr command reads the Expression parameter, evaluates it, and writes the result to standard output. You must apply the following rules to the Expression parameter: Separate each term with blanks. Precede characters special to the shell with a \ (backslash). Quote strings containing blanks or other special characters. The following items describe Expression parameter operators and keywords. Characters that need to be escaped are preceded by a \ (backslash). The items are listed in order of increasing precedence, with equal precedence operators grouped within { } (braces): Fall 2013 CS4350 Husain Gholoom Page 23
24 Item Expression1 \ Expression2 Description Returns Expression1 if it is neither a null value nor a 0 value; otherwise, it returns Expression2. Expression1 \& Expression2 Returns Expression1 if both expressions are neither a null value nor a 0 value; otherwise, it returns a value of 0. Expression1 { =, \>, \>=, \<, \<=,!= } Expression2 Returns the result of an integer comparison if both expressions are integers; otherwise, it returns the result of a string comparison. Expression1 {+, - } Expression2 Adds or subtracts integer-valued arguments. Expression1 { \*, /, % } Expression2 Multiplies, divides, or provides the remainder from the division of integer-valued arguments. Expression1 : Expression2 Compares the string resulting from the evaluation of Expression1 with the regular expression pattern resulting from the evaluation of Expression2. Regular expression syntax is the same as that of the ed command, except that all patterns are anchored to the beginning of the string (that is, only sequences starting at the first character of a string are matched by the regular expression). Therefore, a ^ (caret) is not a special character in this context. Normally the matching operator returns the number of characters matched (0 on failure). If the pattern contains a subexpression, that is: \( Expression \) then a string containing the actual matched characters is returned. Fall 2013 CS4350 Husain Gholoom Page 24
25 Simple Examples #!/bin/bash var1=10 var2=20 var1=$(expr $var1 + 1 ) echo $var1 echo var1=$(expr $var1 \* $var1 ) echo $var1 echo echo $(expr $var1 / 10) echo echo $(expr $var1 % 10) echo expr $var1 = $var2 expr $var1 \< $var2 expr $var1 \!= $var2 exit 0 The Result husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 25
26 (Eval) Arguments as Shell Command in Unix / Linux Eval is a built in linux or unix command. The eval command is used to execute the arguments as a shell command on unix or linux system Eval command comes in handy when you have a unix or linux command stored in a variable and you want to execute that command stored in the string. The following shell script example shows how to use the eval command: Example #!/bin/bash #ls command stored in a variable COMMAND="ls -lrt" #executing the ls command using eval eval $COMMAND exit 0 The Result -rwxr-xr-x@ 1 husaingholoom staff 115 Sep 4 22:24 sc1.sh -rwxr-xr-x 1 husaingholoom staff 327 Sep 7 20:13 sc2.sh -rwxr-xr-x 1 husaingholoom staff 156 Sep 8 12:29 sc4.sh... husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 26
27 The eval command first evaluates the argument and then runs the command stored in the argument. The following example shows how to use the eval command on the unix command line: husain-gholooms-macbook:shell husaingholoom$ eval "rm test.sh" Export export var=value Export the value of var to its global environment so that its child shell can inherit the variable. In terminal export myvar=val1 In a child script echo $myvar Fall 2013 CS4350 Husain Gholoom Page 27
28 Arrays : Defining Array Values: The difference between an array variable and a scalar variable can be explained as follows. Say that you are trying to represent the names of various students as a set of variables. Each of the individual variables is a scalar variable as follows: NAME01="Zara" NAME02="Ryan" NAME03="Gary" NAME04="Ayan" NAME05="Daisy" We can use a single array to store all the above mentioned names. Following is the simplest method of creating an array variable is to assign a value to one of its indices. This is expressed as follows: array_name[index]=value Here array_name is the name of the array, index is the index of the item in the array that you want to set, and value is the value you want to set for that item. As an example, the following commands: NAME[0]="Zara" NAME[1]="Ryan" NAME[2]="Gary" NAME[3]="Ayan" NAME[4]="Daisy" Fall 2013 CS4350 Husain Gholoom Page 28
29 Accessing Array Values: After you have set any array variable, you access it as follows: ${array_name[index]} Here array_name is the name of the array, and index is the index of the value to be accessed. Following is the simplest example: #!/bin/sh NAME[0]="Zara" NAME[1]="Ryan" NAME[2]="Gary" NAME[3]="Ayan" NAME[4]="Daisy" echo "First Index: ${NAME[0]}" echo "Second Index: ${NAME[1]}" This above script will produce following result: $./test.sh First Index: Zara Second Index: Ryan Fall 2013 CS4350 Husain Gholoom Page 29
30 You can access all the items in an array in one of the following ways: echo ${array_name[*]} # show all elements in the array echo ${array_name[@]} echo ${#string[@]} # show array size echo ${NAME[@]:3} # show all elements starting from element 3 echo ${NAME[@]#G*r} # Apply to all elements Matches "Gary" and removes it. echo ${NAME[@]/Zar/XYZ} # Substring Replacement - Replace zar with XYZ - Applied to all elements of the array. Sample Run $./test.sh First Method: Zara Ryan Gary Ayan Daisy Second Method: Zara Ryan Gary Ayan Daisy 5 Fall 2013 CS4350 Husain Gholoom Page 30
31 Ayan Daisy Zara Ryan y Ayan Daisy XYZa Ryan Gary Ayan Daisy What is the output of the following : #!/bin/bash area[11]=23 area[13]=37 area[51]=ufos echo -n "area[11] = " echo ${area[11]} echo -n "area[13] = " echo ${area[13]} echo echo "Contents of area[51] are ${area[51]}." echo -n "area[43] = " echo ${area[43]} echo "(area[43] unassigned)" echo # Sum of two array variables assigned to third area[5]=`expr ${area[11]} + ${area[13]}` echo "area[5] = area[11] + area[13]" echo -n "area[5] = " echo ${area[5]} Fall 2013 CS4350 Husain Gholoom Page 31
32 area[6]=`expr ${area[11]} + ${area[51]}` echo "area[6] = area[11] + area[51]" echo -n "area[6] = " echo ${area[6]} # This fails because adding an integer to a string is not permitted. echo ; echo ; echo # Another array, "area2". # Another way of assigning array variables... # array_name=( XXX YYY ZZZ... ) area2=( zero one two three four ) echo -n "area2[0] = " echo ${area2[0]} # Aha, zero-based indexing (first element of array is [0], not [1]). echo -n "area2[1] = " echo ${area2[1]} array. # [1] is second element of echo; echo; echo Fall 2013 CS4350 Husain Gholoom Page 32
33 # Yet another array, "area3". # Yet another way of assigning array variables... # array_name=([xx]=xxx [yy]=yyy...) area3=([17]=seventeen [24]=twenty-four) echo -n "area3[17] = " echo ${area3[17]} echo -n "area3[24] = " echo ${area3[24]} exit 0 Fall 2013 CS4350 Husain Gholoom Page 33
34 The Result -n area[11] = 23 -n area[13] = 37 Contents of area[51] are UFOs. -n area[43] = (area[43] unassigned) area[5] = area[11] + area[13] -n area[5] = 60 expr: non-numeric argument area[6] = area[11] + area[51] -n area[6] = -n area2[0] = zero -n area2[1] = one -n area3[17] = seventeen -n area3[24] = twenty-four husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 34
35 set The set command sets the parameter variables for the shell. It can be a useful way of using fields in commands that output space-separated values. Suppose you want to use the name of the current month in a shell script. The system provides a date command, which contains the month as a string, but you need to separate it from the other fields. You can do this using a combination of the set command and the $(...) construct to execute the date command and return the result. The date command output has the month string as its second parameter: Example #!/bin/sh echo the date is $(date) set $(date) echo The Day of Week is $1 echo The Month is $2 echo The Day is $3 echo The year is $6 echo The Current Time is $4 echo echo var=$1 Echo $var exit 0 Fall 2013 CS4350 Husain Gholoom Page 35
36 Sample Run husain-gholooms-macbook:shell husaingholoom$./sc16set.sh the date is Sat Sep 14 16:02:23 CDT 2013 The Day of Week is Sat The Month is Sep The Day is 14 The year is 2013 The Current Time is 16:12:15 Sat husain-gholooms-macbook:shell husaingholoom$ Unset NOT "not set" Remove variables from the environment Insert the following code at the end of the previous example unset var echo $var Fall 2013 CS4350 Husain Gholoom Page 36
37 Sample Run husain-gholooms-macbook:shell husaingholoom$./sc16set.sh the date is Sat Sep 14 16:16:35 CDT 2013 The Day of Week is Sat The Month is Sep The Day is 14 The year is 2013 The Current Time is 16:16:35 Sat husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 37
38 Few More Commands The. Command The dot (.) command executes the command in the current shell:../shell_script exec The exec command has two different uses. Its typical use is to replace the current shell with a different program. For example, exec wall Thanks for all the fish in a script will replace the current shell with the wall command. No lines in the script after the exec will be processed, because the shell that was executing the script no longer exists. Fall 2013 CS4350 Husain Gholoom Page 38
39 printf The printf command is available only in more recent shells. X/Open suggests that it should be used in preference to echo for generating formatted output, though few people seem to follow this advice. The syntax is Printf format string parameter1 parameter2... The format string is very similar to that used in C or C++, with some restrictions. Principally, floating point isn t supported, because all arithmetic in the shell is performed as integers. The format string con- sists of any combination of literal characters, escape sequences, and conversion specifiers. All characters in the format string other than % and \ appear literally in the output. The following escape sequences are supported: Escape Sequence Description \ Double quote \\ Backslash character \a Alert (ring the bell or beep) \b Backspace character \c Suppress further output \f Form feed character \n Newline character \r Carriage return \t Tab character \v Vertical tab character \ooo The single character with octal value ooo \xhh The single character with the hexadecimal value HH Fall 2013 CS4350 Husain Gholoom Page 39
40 Description D Output a decimal number. C Output a character. S Output a string. % Output the % character. $ printf %s\n hello hello $ printf %s %d\t%s Hi There 15 people Hi There 15 people In a Script #!/bin/sh printf "%d\n" printf "%d\n" \'A printf "%d\n" "'B" # no argument is specified Sample run Fall 2013 CS4350 Husain Gholoom Page 40
41 Functions in Shell Functions are normally used if a piece of code is repeated at various places in a script. Before you invoke a function, you must define it. A function is invoked by using its name as a command. Another alternative is to create another script file for the block of code and invoke this code by calling the script. Hard to modify the variables in the invoking shell. Take more time to read and execute. Function_name () { command... } Functions are called, triggered, simply by invoking their names. A function call is equivalent to a command. Fall 2013 CS4350 Husain Gholoom Page 41
42 Example #!/bin/bash JUST_A_SECOND=1 funky () { # This is about as simple as functions get. echo "This is a funky function." echo "Now exiting funky function." } # Function declaration must precede call. fun () { # A somewhat more complex function. i=0 REPEATS=3 echo echo "And now the fun really begins." echo sleep $JUST_A_SECOND # Hey, wait a second! while [ $i -lt $REPEATS ] do echo " FUNCTIONS >" echo "< ARE " echo "< FUN >" echo let "i+=1" done } # Now, call the functions. funky fun Fall 2013 CS4350 Husain Gholoom Page 42
43 Sample Run This is a funky function. Now exiting funky function. And now the fun really begins FUNCTIONS > < ARE < FUN > FUNCTIONS > < ARE < FUN > FUNCTIONS > < ARE < FUN > husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 43
44 Passing Parameters to Functions : #!/bin/bash # Functions and parameters DEFAULT=default func2 () { if [ -z "$1" ] then echo "-Parameter #1 is zero length.-" else echo "-Parameter #1 is \"$1\".-" fi # Default param value. # Is parameter #1 zero length? # Or no parameter passed. variable=${1-$default} # What does echo "variable = $variable" #+ parameter substitution show? # # It distinguishes between #+ no param and a null param. if [ "$2" ] then echo "-Parameter #2 is \"$2\".-" fi return 0 } echo echo "Nothing passed." func2 echo # Called with no params Fall 2013 CS4350 Husain Gholoom Page 44
45 echo "Zero-length parameter passed." func2 "" echo echo "One parameter passed." func2 first echo echo "Two parameters passed." func2 first second echo # Called with zero-length param # Called with one param # Called with two params echo "\"\" \"second\" passed." func2 "" second echo # Called with zero-length first parameter # and ASCII string as a second one. exit 0 Fall 2013 CS4350 Husain Gholoom Page 45
46 Sample Run husain-gholooms-macbook:shell husaingholoom$./sc19functions.sh Nothing passed. -Parameter #1 is zero length.- variable = default Zero-length parameter passed. -Parameter #1 is zero length.- variable = One parameter passed. -Parameter #1 is "first".- variable = first Two parameters passed. -Parameter #1 is "first".- variable = first -Parameter #2 is "second".- "" "second" passed. -Parameter #1 is zero length.- variable = -Parameter #2 is "second".- husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 46
47 Function with return values!/bin/bash # Maximum of two integers. E_PARAM_ERR=250 EQUAL=251 # If less than 2 params passed to function. # Return value if both params equal. # Error values out of range of any #+ params that might be fed to the function. max2 () # Returns larger of two numbers. { # Note: numbers compared must be less than 250. if [ -z "$2" ] then return $E_PARAM_ERR fi if [ "$1" -eq "$2" ] then return $EQUAL else if [ "$1" -gt "$2" ] then return $1 else return $2 fi fi } Fall 2013 CS4350 Husain Gholoom Page 47
48 echo This Script finds the largest of two integers echo Enter 2 Integers read var1 var2 #max max2 $var1 $var2 return_val=$? if [ "$return_val" -eq $E_PARAM_ERR ] then echo "Need to pass two parameters to the function." elif [ "$return_val" -eq $EQUAL ] then echo "The two numbers are equal." else echo "The larger of the two numbers is $return_val." fi exit 0 Fall 2013 CS4350 Husain Gholoom Page 48
49 Sample Run husain-gholooms-macbook:shell husaingholoom$./sc20functions.sh This Script find the largest of two integers Enter 2 Integers The larger of the two numbers is 20. husain-gholooms-macbook:shell husaingholoom$./sc20functions.sh This Script find the largest of two integers Enter 2 Integers The two numbers are equal. husain-gholooms-macbook:shell husaingholoom$./sc20functions.sh This Script find the largest of two integers Enter 2 Integers 10 Need to pass two parameters to the function. husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 49
50 Local and Global Variables in Functions #!/bin/bash # Global and local variables inside a function. func () { local loc_var=23 echo echo "\"loc_var\" in function = $loc_var" # Declared as local variable. # Uses the 'local' builtin. global_var=999 # Not declared as local. # Therefore, defaults to global. } echo "\"global_var\" in function = $global_var" func # Now, to see if local variable "loc_var" exists outside the function. echo echo "\"loc_var\" outside function = $loc_var" # $loc_var outside function = # No, $loc_var not visible globally. echo "\"global_var\" outside function = $global_var" # $global_var outside function = 999 # $global_var is visible globally. echo exit 0 Fall 2013 CS4350 Husain Gholoom Page 50
51 Sample Run husain-gholooms-macbook:shell husaingholoom$./sc21functions.sh "loc_var" in function = 23 "global_var" in function = 999 "loc_var" outside function = "global_var" outside function = 999 husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 51
52 Find find path -name pattern option Find a file at the path with the name according to the option Option - type f - type d - newer otherfile - user username - man find find. -name mytest ls -l $(find. -name mytest ) [hag10@zeus ~]$ find. -name grp.sh [hag10@zeus ~]$ ls -ls $(find. -name grp.sh ) Fall 2013 CS4350 Husain Gholoom Page 52
53 Result is./grp.sh 8 -rwx hag10 faculty 99 Sep 18 11:10./grp.sh Filename Expansion Special characters for filename patterns: -? match any single character. file? matches file1 filea but does not match file12 * match any string, including the null string. file* matches anything with a prefix file (file, file12) - [ ] match any one of the enclosed characters. file[12] matches file1 and file2 only file[1 9] matches file1, file2,..., file9 - [^ ] match any except the enclosed characters. Assume the filenames: a1.tex a.c a1.ps ab ab.txt - ls a?.* will be expanded by the shell to - ls a1.tex a1.ps ab.txt Fall 2013 CS4350 Husain Gholoom Page 53
54 Grep grep [options] PATTERN [FILE...] Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. Option - nooption : print the lines that match - -c : print the number of lines that match - -i : ignore case - -l : list the name of files that have the lines - -v : select non-matching lines Script File * greptest * grep ommands #!/bin/sh echo $tmp we who echo echo all grep echo greptest.sh grep -c echo greptest.sh grep -l echo greptest.sh grep -i Echo greptest.sh grep "echo echo" greptest.sh grep -v "echo " greptest.sh Fall 2013 CS4350 Husain Gholoom Page 54
55 Sample Run echo $temp who echo echo 2 grep.sh echo $temp who echo echo who echo echo #!/bin/sh we all husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 55
56 Pattern The. Any single character Not?, but \? Script File * greptest * grep ommands #!/bin/sh echo $tmp we who echo echo grep "e\?ho" greptest.sh grep "e.ho" greptest.sh all Sample Run echo $temp who echo echo echo $temp who echo echo husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 56
57 [ ] and [^ ] A range of characters bread[a-z]bread - breadebread match - bread[abcd]bread no match - bread1bread no match bread[a-za-z]bread - breadbbread match bread[abcd]bread - breadebread no match The * is Kleene star Match zero or more of previous character ba*rgh matches: - brgh - baargh - baaaargh - baaaaaaaargh - baaaaaaaaaaargh Fall 2013 CS4350 Husain Gholoom Page 57
58 The + The \+ is similar, but Match one or more of previous character ba\+rgh again: - brgh - no match! - baargh - baaaargh - baaaaaaaargh - baaaaaaaaaaargh The \( \) \( \) packages characters ( 0 or more ) - bread_\(bacon\)*_bread - bread bread match - bread_bacon_bread match - bread_baconbacon_bread match bread_\(bacon\)\+_bread ( 1 or more ) - bread bread no match! - bread_bacon_bread match - bread_baconbacon_bread match Fall 2013 CS4350 Husain Gholoom Page 58
59 The \{ \} \{n\} The preceding item is matched exactly n times. \{n,\} The preceding item is matched n or more times. \{n,m\} The preceding item is matched at least n times, but not more than m times. The \ \ is the choice character bread_\(a\ b\)_bread - bread_a_bread match - bread_b_bread match bread_\(avocado\ bacon\)_bread - bread_avocado_bread - bread_bacon_bread Fall 2013 CS4350 Husain Gholoom Page 59
60 The $ The end of a line bread$ - breadabread - match - breadbbread - match - bread1 - no match ^ The beginning of a line ^bread - breadabread - match - breadbbread - match - 1bread - no match Fall 2013 CS4350 Husain Gholoom Page 60
61 [: :] Match Pattern Meaning [:alnum:] - alphanumeric [:alpha:] - letter [:cntrl:] - ASCII control [:digit:] - digits [:graph:] - noncontrol, nonspace [:lower:] - lowercase [:print:] - printable [:punct:] - punctuation characters [:space:] - space or tab [:upper:] - uppercase [:xdigit:] - hexadecimal digits Example : Assume that you have the following text in a file called greword.txt Is this a dagger which I see before me, A dagger of the mind, a false creation, Moves like a ghost. Thou sure and firm-set earth. Fall 2013 CS4350 Husain Gholoom Page 61
62 Suppose that you would like to find a line that contains the letter d followed by space. your command will look something like this shell husaingholoom$ grep d[[:space:]] grepword.txt The Result will be Moves like a ghost. Thou sure and firm-set earth. Another Example shell husaingholoom$ grep [[:space:]] grepword.txt or [hag10@zeus ~]$ grep echo[[:blank:]] test2.sh Fall 2013 CS4350 Husain Gholoom Page 62
63 The Result will be A dagger of the mind, a false creation, Or Is this a dagger which I see before me, A dagger of the mind, a false creation, Moves like a ghost. Thou sure and firm-set earth. Or echo "${NAME[0]}" echo "${NAME[1]}" echo "${NAME[2]}" echo "${NAME[*]}" echo ${NAME[@]:3} echo "${#string[@]}" Fall 2013 CS4350 Husain Gholoom Page 63
64 ` ` The output of the execution of a command `ls` is the same as (ls) is the list of the files in the directory $(( )) Evaluate arithmetic expression Similar to expr x=$((10 + 2)) x=$(expr ) x=$((10-2)) x=$(expr 10-2) x=$((10 * 2)) x=$(expr 10 \* 2) x=$((10 / 2)) (10+2) x=$(expr 10 / 2) expr Fall 2013 CS4350 Husain Gholoom Page 64
65 ${ } The range of variable name #!/bin/sh my=10 mymoney=20 echo ${my}money echo $mymoney Sample Run 10money 20 husain-gholooms-macbook:shell husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 65
66 ${ : } echo ${ } before after after foo foo echo ${foo:-bar} null null bar Set the value of foo to be bar if foo is null ${foo:=bar} null bar bar Set the value of foo to be bar ${foo:?bar} null null foo:bar print foo:bar and set the value of foo to be null ${foo:+bar} foo foo bar returns bar if foo does exist and not null ${ # } The length of a variable's value myvar=12234 echo ${#myvar} 5 Fall 2013 CS4350 Husain Gholoom Page 66
67 ${ % } Remove the smallest matching part from the end foo= echo ${foo%3} echo ${foo%3*} 123 echo ${foo%3?} ${ %% } Remove the largest matching part from the end ${ # } foo= echo ${foo%%3} echo ${foo%%3*} 12 echo ${foo%%3?} Remove the smallest matching part from the beginning foo= echo ${foo#3} echo ${foo#*3} 356 echo ${foo#?3} Fall 2013 CS4350 Husain Gholoom Page 67
68 ${ ## } Remove the largest matching part from the beginning foo= echo ${foo##3} echo ${foo##*3} 56 echo ${foo##?3} Redirect and Pipe Redirect input - cmd < input ( more < file.txt ) Redirect output - cmd > output ( ls l > lsout.txt ) - cmd >> output ( ls l >> lsout2.txt ) Pipe : inter-process - cmd1 cmd2 Fall 2013 CS4350 Husain Gholoom Page 68
69 We can connect processes together using the pipe operator ( ). For example, the following program means run the ps program, sort its output, and save it in the file pssort.out ps sort > pssort.out The sort command will sort the list of words in a textfile into alphbetical order according to the ASCII code set character order. All Other commands man bash Fall 2013 CS4350 Husain Gholoom Page 69
70 Shell Programs Just typing the shell script on the file is a quick and easy way of trying out small code fragments, and is very useful while you are learning or just testing things out. Suppose you have a large number of.sh or C files and wish to examine the files that contain the string echo or printf statements. Rather than search using the grep command for the string in the files and then list the files individually, you could perform the whole operation in an script like this: #!/bin/sh for file in * do if grep -l echo $file then more $file fi done Fall 2013 CS4350 Husain Gholoom Page 70
71 Or, you could perform the whole operation in an interactive script like this: $ for file in * > do > if grep -l echo $file > then > more $file > fi > done Sample Run grp.sh #!/bin/sh for file in * do if grep -l echo $file then more $file fi done test2.sh #!/bin/sh NAME[0]="Zara" NAME[1]="Ryan" echo "${NAME[0]}" echo "${NAME[1]}" echo "${NAME[2]}" echo "${NAME[*]}" echo ${NAME[@]:3} echo "${#string[@]}" exit 0 Fall 2013 CS4350 Husain Gholoom Page 71
Bourne Shell Reference
> Linux Reviews > Beginners: Learn Linux > Bourne Shell Reference Bourne Shell Reference found at Br. David Carlson, O.S.B. pages, cis.stvincent.edu/carlsond/cs330/unix/bshellref - Converted to txt2tags
More informationTitle:[ Variables Comparison Operators If Else Statements ]
[Color Codes] Environmental Variables: PATH What is path? PATH=$PATH:/MyFolder/YourStuff?Scripts ENV HOME PWD SHELL PS1 EDITOR Showing default text editor #!/bin/bash a=375 hello=$a #No space permitted
More informationShell Programming (bash)
Shell Programming Shell Programming (bash) Commands run from a file in a subshell A great way to automate a repeated sequence of commands. File starts with #!/bin/bash absolute path to the shell program
More informationShells & Shell Programming (Part B)
Shells & Shell Programming (Part B) Software Tools EECS2031 Winter 2018 Manos Papagelis Thanks to Karen Reid and Alan J Rosenthal for material in these slides CONTROL STATEMENTS 2 Control Statements Conditional
More informationShells and Shell Programming
Shells and Shell Programming 1 Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed
More informationLinux Shell Scripting. Linux System Administration COMP2018 Summer 2017
Linux Shell Scripting Linux System Administration COMP2018 Summer 2017 What is Scripting? Commands can be given to a computer by entering them into a command interpreter program, commonly called a shell
More informationConditional Control Structures. Dr.T.Logeswari
Conditional Control Structures Dr.T.Logeswari TEST COMMAND test expression Or [ expression ] Syntax Ex: a=5; b=10 test $a eq $b ; echo $? [ $a eq $b] ; echo $? 2 Unix Shell Programming - Forouzan 2 TEST
More informationShells and Shell Programming
Shells and Shell Programming Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed
More informationAssignment clarifications
Assignment clarifications How many errors to print? at most 1 per token. Interpretation of white space in { } treat as a valid extension, involving white space characters. Assignment FAQs have been updated.
More informationCisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012
Cisco IOS Shell Last Updated: December 14, 2012 The Cisco IOS Shell (IOS.sh) feature provides shell scripting capability to the Cisco IOS command-lineinterface (CLI) environment. Cisco IOS.sh enhances
More informationA Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers
A Big Step Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Copyright 2006 2009 Stewart Weiss What a shell really does Here is the scoop on shells. A shell is a program
More informationBash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013
Bash scripting Tutorial Super User Programming & Scripting 22 March 2013 Hello World Bash Shell Script First you need to find out where is your bash interpreter located. Enter the following into your command
More informationA shell can be used in one of two ways:
Shell Scripting 1 A shell can be used in one of two ways: A command interpreter, used interactively A programming language, to write shell scripts (your own custom commands) 2 If we have a set of commands
More informationEECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done
Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who
More informationUNIX shell scripting
UNIX shell scripting EECS 2031 Summer 2014 Przemyslaw Pawluk June 17, 2014 What we will discuss today Introduction Control Structures User Input Homework Table of Contents Introduction Control Structures
More informationScripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers
Scripting Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Adapted from Practical Unix and Programming Hunter College Copyright 2006 2009 Stewart Weiss What a shell
More informationCSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science
CSCI 211 UNIX Lab Shell Programming Dr. Jiang Li Why Shell Scripting Saves a lot of typing A shell script can run many commands at once A shell script can repeatedly run commands Help avoid mistakes Once
More informationBashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009
Bashed One Too Many Times Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009 What is a Shell? The shell interprets commands and executes them It provides you with an environment
More informationLinux shell programming for Raspberry Pi Users - 2
Linux shell programming for Raspberry Pi Users - 2 Sarwan Singh Assistant Director(S) NIELIT Chandigarh 1 SarwanSingh.com Education is the kindling of a flame, not the filling of a vessel. - Socrates SHELL
More information9.2 Linux Essentials Exam Objectives
9.2 Linux Essentials Exam Objectives This chapter will cover the topics for the following Linux Essentials exam objectives: Topic 3: The Power of the Command Line (weight: 10) 3.3: Turning Commands into
More informationShell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.
Shell Scripts A shell script contains a sequence of commands in a text file. Shell is an command language interpreter. Shell executes commands read from a file. Shell is a powerful programming available
More informationBash scripting basics
Bash scripting basics prepared by Anatoliy Antonov for ESSReS community September 2012 1 Outline Definitions Foundations Flow control References and exercises 2 Definitions 3 Definitions Script - [small]
More informationShell programming. Introduction to Operating Systems
Shell programming Introduction to Operating Systems Environment variables Predened variables $* all parameters $# number of parameters $? result of last command $$ process identier $i parameter number
More informationVi & Shell Scripting
Vi & Shell Scripting Comp-206 : Introduction to Week 3 Joseph Vybihal Computer Science McGill University Announcements Sina Meraji's office hours Trottier 3rd floor open area Tuesday 1:30 2:30 PM Thursday
More information1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03
1 of 18 21/10/2554 9:39 Bash scripting Tutorial tar -czf myhome_directory.tar.gz /home/linuxcong Last Updated on Wednesday, 13 April 2011 08:03 Article Index 1. Hello World Bash Shell Script 2. Simple
More informationПрограмиранев UNIX среда
Програмиранев UNIX среда Използванена команден шел и създаванена скриптове: tcsh, bash, awk, python Shell programming As well as using the shell to run commands you can use its built-in programming language
More informationMore Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1
More Scripting and Regular Expressions Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 Regular Expression Summary Regular Expression Examples Shell Scripting 2 Do not confuse filename globbing
More informationsottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi
Titolo presentazione Piattaforme Software per la Rete sottotitolo BASH Scripting Milano, XX mese 20XX A.A. 2016/17, Alessandro Barenghi Outline 1) Introduction to BASH 2) Helper commands 3) Control Flow
More informationLING 408/508: Computational Techniques for Linguists. Lecture 5
LING 408/508: Computational Techniques for Linguists Lecture 5 Last Time Installing Ubuntu 18.04 LTS on top of VirtualBox Your Homework 2: did everyone succeed? Ubuntu VirtualBox Host OS: MacOS or Windows
More informationShell Scripting. Todd Kelley CST8207 Todd Kelley 1
Shell Scripting Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 If we have a set of commands that we want to run on a regular basis, we could write a script A script acts as a Linux command,
More informationEssentials for Scientific Computing: Bash Shell Scripting Day 3
Essentials for Scientific Computing: Bash Shell Scripting Day 3 Ershaad Ahamed TUE-CMS, JNCASR May 2012 1 Introduction In the previous sessions, you have been using basic commands in the shell. The bash
More informationLast Time. on the website
Last Time on the website Lecture 6 Shell Scripting What is a shell? The user interface to the operating system Functionality: Execute other programs Manage files Manage processes Full programming language
More informationCommand Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.
Command Interpreters A command interpreter is a program that executes other programs. Aim: allow users to execute the commands provided on a computer system. Command interpreters come in two flavours:
More informationAdvanced Unix Programming Module 03 Raju Alluri spurthi.com
Advanced Unix Programming Module 03 Raju Alluri askraju @ spurthi.com Advanced Unix Programming: Module 3 Shells & Shell Programming Environment Variables Writing Simple Shell Programs (shell scripts)
More informationShell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala
Shell scripting and system variables HORT 59000 Lecture 5 Instructor: Kranthi Varala Text editors Programs built to assist creation and manipulation of text files, typically scripts. nano : easy-to-learn,
More informationReview. Preview. Functions. Functions. Functions with Local Variables. Functions with Local Variables 9/11/2017
Review Preview Conditions The test, or [ Command Control Structures if statement if-else-if statement for loop statement while loop statement until loop statement case statement Functions Function with
More informationbash, part 3 Chris GauthierDickey
bash, part 3 Chris GauthierDickey More redirection As you know, by default we have 3 standard streams: input, output, error How do we redirect more than one stream? This requires an introduction to file
More informationCOMP 4/6262: Programming UNIX
COMP 4/6262: Programming UNIX Lecture 12 shells, shell programming: passing arguments, if, debug March 13, 2006 Outline shells shell programming passing arguments (KW Ch.7) exit status if (KW Ch.8) test
More information12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script
12 Shell Programming This chapter concentrates on shell programming. It explains the capabilities of the shell as an interpretive high-level language. It describes shell programming constructs and particulars.
More informationMore Scripting Techniques Scripting Process Example Script
More Scripting Techniques Scripting Process Example Script 1 arguments to scripts positional parameters input using read exit status test program, also known as [ if statements error messages 2 case statement
More informationUnix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT
Unix as a Platform Exercises + Solutions Course Code: OS 01 UNXPLAT Working with Unix Most if not all of these will require some investigation in the man pages. That's the idea, to get them used to looking
More informationUseful Unix Commands Cheat Sheet
Useful Unix Commands Cheat Sheet The Chinese University of Hong Kong SIGSC Training (Fall 2016) FILE AND DIRECTORY pwd Return path to current directory. ls List directories and files here. ls dir List
More informationShell Start-up and Configuration Files
ULI101 Week 10 Lesson Overview Shell Start-up and Configuration Files Shell History Alias Statement Shell Variables Introduction to Shell Scripting Positional Parameters echo and read Commands if and test
More informationbash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017
bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 Command Lists A command is a sequence of commands separated by the operators ; & && and ; is used to simply execute commands in
More informationLinux shell scripting Getting started *
Linux shell scripting Getting started * David Morgan *based on chapter by the same name in Classic Shell Scripting by Robbins and Beebe What s s a script? text file containing commands executed as a unit
More informationBasic Linux (Bash) Commands
Basic Linux (Bash) Commands Hint: Run commands in the emacs shell (emacs -nw, then M-x shell) instead of the terminal. It eases searching for and revising commands and navigating and copying-and-pasting
More informationIntroduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University
Introduction to Bash Programming Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University 1 Outline Shell command line syntax Shell builtin commands Shell variables, arguments
More informationS E C T I O N O V E R V I E W
AN INTRODUCTION TO SHELLS S E C T I O N O V E R V I E W Continuing from last section, we are going to learn about the following concepts: understanding quotes and escapes; considering the importance of
More informationCS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash
CS 25200: Systems Programming Lecture 10: Shell Scripting in Bash Dr. Jef Turkstra 2018 Dr. Jeffrey A. Turkstra 1 Lecture 10 Getting started with Bash Data types Reading and writing Control loops Decision
More informationComputer Systems and Architecture
Computer Systems and Architecture Stephen Pauwels UNIX Scripting Academic Year 2018-2019 Outline Basics Conditionals Loops Advanced Exercises Shell Scripts Grouping commands into a single file Reusability
More informationShell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong
Shell Prof. Jinkyu Jeong (Jinkyu@skku.edu) TA -- Minwoo Ahn (minwoo.ahn@csl.skku.edu) TA -- Donghyun Kim (donghyun.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu
More informationLOG ON TO LINUX AND LOG OFF
EXPNO:1A LOG ON TO LINUX AND LOG OFF AIM: To know how to logon to Linux and logoff. PROCEDURE: Logon: To logon to the Linux system, we have to enter the correct username and password details, when asked,
More informationIntroduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala
Introduction to Linux Basics Part II 1 Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala pakala@uga.edu 2 Variables in Shell HOW DOES LINUX WORK? Shell Arithmetic I/O and
More informationShell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh
Shell Programming Shells A shell can be used in one of two ways: A command interpreter, used interactively A programming language, to write shell scripts (your own custom commands) Shell Scripts A shell
More informationOutput with printf Input. from a file from a command arguments from the command read
More Scripting 1 Output with printf Input from a file from a command arguments from the command read 2 A script can test whether or not standard input is a terminal [ -t 0 ] What about standard output,
More informationUnix Scripts and Job Scheduling. Overview. Running a Shell Script
Unix Scripts and Job Scheduling Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu/~spring Overview Shell Scripts
More informationUNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012
What Is a Shell? UNIX Shell Scripts CSE 2031 Fall 2012 A program that interprets your requests to run other programs Most common Unix shells: Bourne shell (sh) C shell (csh - tcsh) Korn shell (ksh) Bourne-again
More informationCSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019
CSCI 2132: Software Development Shell Scripting Norbert Zeh Faculty of Computer Science Dalhousie University Winter 2019 Reading Glass and Ables, Chapter 8: bash Your Shell vs Your File Manager File manager
More informationWhile Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example
While Statement (35.15) General form. The commands in the loop are performed while the condition is true. while condition one-or-more-commands While Statement Examples # process commands until a stop is
More informationbash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017
bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 Command Lists A command is a sequence of commands separated by the operators ; & && and ; is used to simply execute commands in
More informationMore Scripting Todd Kelley CST8207 Todd Kelley 1
More Scripting Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 Arithmetic Output with printf Input from a file from a command CST8177 Todd Kelley 2 A script can test whether or not standard
More information% echo $SHELL /usr/local/bin/bash. % sh $
% echo $SHELL /usr/local/bin/bash % sh $ #!/bin/sh chmod +x test.sh./test.sh my=test export my set my=test setenv my test $ export PAGER=/usr/bin/less % setenv PAGER /usr/bin/less $ current_month=`date
More informationReview of Fundamentals
Review of Fundamentals 1 The shell vi General shell review 2 http://teaching.idallen.com/cst8207/14f/notes/120_shell_basics.html The shell is a program that is executed for us automatically when we log
More informationbash Execution Control COMP2101 Winter 2019
bash Execution Control COMP2101 Winter 2019 Bash Execution Control Scripts commonly can evaluate situations and make simple decisions about actions to take Simple evaluations and actions can be accomplished
More informationShell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting
Shell script/program Basic shell scripting CS 2204 Class meeting 5 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002 A series of shell commands placed in an ASCII text file Commands include
More informationRegex, Sed, Awk. Arindam Fadikar. December 12, 2017
Regex, Sed, Awk Arindam Fadikar December 12, 2017 Why Regex Lots of text data. twitter data (social network data) government records web scrapping many more... Regex Regular Expressions or regex or regexp
More informationIntroduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi
Bash Scripting Alessandro Barenghi Dipartimento di Elettronica, Informazione e Bioingegneria Politecnico di Milano alessandro.barenghi - at - polimi.it April 28, 2015 Introduction The bash command shell
More informationChapter 5 The Bourne Shell
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003 Notes by Bing Li Creating/Assigning a Variable Name=value Variable created
More informationexample: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.
Bourne Shell Programming Variables - creating and assigning variables Bourne shell use the set and unset to create and assign values to variables or typing the variable name, an equal sign and the value
More informationTable of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs
Summer 2010 Department of Computer Science and Engineering York University Toronto June 29, 2010 1 / 36 Table of contents 1 2 3 4 2 / 36 Our goal Our goal is to see how we can use Unix as a tool for developing
More informationEECS2301. Lab 1 Winter 2016
EECS2301 Lab 1 Winter 2016 Lab Objectives In this lab, you will be introduced to the Linux operating system. The basic commands will be presented in this lab. By the end of you alb, you will be asked to
More informationBasic Shell Scripting
Basic Shell Scripting Wei Feinstein HPC User Services LSU HPC & LON sys-help@loni.org February 2018 Outline Introduction to Linux Shell Shell Scripting Basics Variables Quotations Beyond Basic Shell Scripting
More informationUNIX System Programming Lecture 3: BASH Programming
UNIX System Programming Outline Filesystems Redirection Shell Programming Reference BLP: Chapter 2 BFAQ: Bash FAQ BMAN: Bash man page BPRI: Bash Programming Introduction BABS: Advanced Bash Scripting Guide
More informationTopic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes
Topic 2: More Shell Skills Sub-Topic 1: Quoting Sub-topics: 1 quoting 2 shell variables 3 sub-shells 4 simple shell scripts (no ifs or loops yet) 5 bash initialization files 6 I/O redirection & pipes 7
More informationGNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer.
Concise! GNU Bash http://talk.jpnc.info/bash_lfnw_2017.pdf an introduction to advanced usage James Pannacciulli Systems Engineer Notes about the presentation: This talk assumes you are familiar with basic
More informationLecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?
Lecture 4 Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions? Tuesday, September 7 CS 375 UNIX System Programming - Lecture
More informationUnix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP
Unix Guide Meher Krishna Patel Created on : Octorber, 2017 Last updated : December, 2017 More documents are freely available at PythonDSP Table of contents Table of contents i 1 Unix commands 1 1.1 Unix
More informationIntroduction: What is Unix?
Introduction Introduction: What is Unix? An operating system Developed at AT&T Bell Labs in the 1960 s Command Line Interpreter GUIs (Window systems) are now available Introduction: Unix vs. Linux Unix
More informationSHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo
SHELL SCRIPT BASIC UNIX Programming 2014 Fall by Euiseong Seo Shell Script Interactive shell sequentially executes a series of commands Some tasks are repetitive and automatable They are what programs
More informationCS Unix Tools & Scripting
Cornell University, Spring 2014 1 February 21, 2014 1 Slides evolved from previous versions by Hussam Abu-Libdeh and David Slater Announcement HW 4 is out. Due Friday, February 28, 2014 at 11:59PM. Wrapping
More informationGNU Bash. An Introduction to Advanced Usage. James Pannacciulli Systems (mt) Media Temple
GNU Bash An Introduction to Advanced Usage James Pannacciulli Systems Engineer @ (mt) Media Temple http://talk.jpnc.info/bash_oscon_2014.pdf Notes about the presentation: This talk assumes you are familiar
More informationBash Programming. Student Workbook
Student Workbook Bash Programming Published by ITCourseware, LLC, 7245 South Havana Street, Suite 100, Englewood, CO 80112 Contributing Authors: Julie Johnson, Rob Roselius Editor: Jeff Howell Special
More informationEECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018
EECS 470 Lab 5 Linux Shell Scripting Department of Electrical Engineering and Computer Science College of Engineering University of Michigan Friday, 1 st February, 2018 (University of Michigan) Lab 5:
More informationBash Reference Manual Reference Documentation for Bash Edition 2.5b, for Bash Version 2.05b. July 2002
.tex Bash Reference Manual Reference Documentation for Bash Edition 2.5b, for Bash Version 2.05b. July 2002 Chet Ramey, Case Western Reserve University Brian Fox, Free Software Foundation Copyright c 1991-2002
More information22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University
Lecture 8 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control 22-Sep-2017 Location: Goldberg CS 127 Time: 14:35 15:25 Instructor:
More informationEECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2
Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who
More informationTopic 2: More Shell Skills
Topic 2: More Shell Skills Sub-topics: 1 quoting 2 shell variables 3 sub-shells 4 simple shell scripts (no ifs or loops yet) 5 bash initialization files 6 I/O redirection & pipes 7 aliases 8 text file
More informationLab 4: Shell scripting
Lab 4: Shell scripting Comp Sci 1585 Data Structures Lab: Tools Computer Scientists Outline 1 2 3 4 5 6 What is shell scripting good? are the duct tape and bailing wire of computer programming. You can
More informationLog into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?
Lecture 4 Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions? Tuesday, September 8 CS 375 UNIX System Programming - Lecture 4 1 Outline Exercise from last lecture More
More informationPreview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019
Review Shell Scripts How to make executable How to change mode Shell Syntax Variables Quoting Environment Variables Parameter Variables Preview Conditions The test, or [ Command if statement if--if statement
More informationEssential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02
Essential Unix (and Linux) for the Oracle DBA Revision no.: PPT/2K403/02 Architecture of UNIX Systems 2 UNIX System Structure 3 Operating system interacts directly with Hardware Provides common services
More informationCSC 2500: Unix Lab Fall 2016
CSC 2500: Unix Lab Fall 2016 Control Statements in Shell Scripts: Decision Mohammad Ashiqur Rahman Department of Computer Science College of Engineering Tennessee Tech University Agenda User Input Special
More informationCS Unix Tools & Scripting
Cornell University, Spring 2014 1 February 24, 2014 1 Slides evolved from previous versions by Hussam Abu-Libdeh and David Slater A note on awk for (item in array) The order in which items are returned
More informationTopic 2: More Shell Skills
Topic 2: More Shell Skills Sub-topics: simple shell scripts (no ifs or loops yet) sub-shells quoting shell variables aliases bash initialization files I/O redirection & pipes text file formats 1 Reading
More informationWelcome to Linux. Lecture 1.1
Welcome to Linux Lecture 1.1 Some history 1969 - the Unix operating system by Ken Thompson and Dennis Ritchie Unix became widely adopted by academics and businesses 1977 - the Berkeley Software Distribution
More informationEECS 2031E. Software Tools Prof. Mokhtar Aboelaze
EECS 2031 Software Tools Prof. Mokhtar Aboelaze Footer Text 1 EECS 2031E Instructor: Mokhtar Aboelaze Room 2026 CSEB lastname@cse.yorku.ca x40607 Office hours TTH 12:00-3:00 or by appointment 1 Grading
More informationReview of Fundamentals. Todd Kelley CST8207 Todd Kelley 1
Review of Fundamentals Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 GPL the shell SSH (secure shell) the Course Linux Server RTFM vi general shell review 2 These notes are available on
More informationCOMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy
COMP 2718: Shell Scripts: Part 1 By: Dr. Andrew Vardy Outline Shell Scripts: Part 1 Hello World Shebang! Example Project Introducing Variables Variable Names Variable Facts Arguments Exit Status Branching:
More informationProcesses and Shells
Shell ls pico httpd CPU Kernel Disk NIC Processes Processes are tasks run by you or the OS. Processes can be: shells commands programs daemons scripts Shells Processes operate in the context of a shell.
More informationSHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo
SHELL SCRIPT BASIC UNIX Programming 2015 Fall by Euiseong Seo Shell Script! Interactive shell sequentially executes a series of commands! Some tasks are repetitive and automatable! They are what programs
More information