Document Details

Uploaded by Deleted User

KIIT

Tanmoy Maitra

Tags

shell programming bash scripting programming computer science

Summary

These notes cover shell programming, specifically focusing on variables, system variables, expressions and if statements. It provides examples of user-defined and system-defined variables, and explains commands and operators commonly used in shell programming. The notes include detailed content and examples for clarity.

Full Transcript

Shell Programming by Tanmoy Maitra, KIIT Variables User defined System defined System defined System Variable Meaning To View Variable Value Type Holds the version of this BASH_VERSION...

Shell Programming by Tanmoy Maitra, KIIT Variables User defined System defined System defined System Variable Meaning To View Variable Value Type Holds the version of this BASH_VERSION echo $BASH_VERSION instance of bash. The name of the your HOSTNAME echo $HOSTNAME computer. The search path for the cd CDPATH echo $CDPATH command. The name of the file in which HISTFILE echo $HISTFILE command history is saved. The maximum number of HISTFILESIZE lines contained in the history echo $HISTFILESIZE file. The number of commands to remember in the command HISTSIZE echo $HISTSIZE history. The default value is 500. System defined System Meaning To View Variable Variable Value Type HOME The home directory of the current user. echo $HOME The Internal Field Separator that is used for word splitting after expansion and to split lines into words IFS echo $IFS with the read builtin command. The default value is. Used to determine the locale category for any LANG category not specifically selected with a variable echo $LANG starting with LC_. The search path for commands. It is a PATH colon-separated list of directories in which the shell echo $PATH looks for commands. System defined System Meaning To View Variable Variable Value Type PS1 Your prompt settings. echo $PS1 The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the TMOUT echo $TMOUT number of seconds to wait for input after issuing the command. If not input provided it will logout user. echo $TERM TERM Your login terminal type. export TERM=vt100 SHELL Set path to login shell. echo $SHELL echo $DISPLAY DISPLAY Set X display name export DISPLAY=:0.1 export EDITOR Set name of default text editor. EDITOR=/usr/bin/ vim User defined 1. a=50 # predefined value echo “$a” 2. read a b c # user input from terminal 3. read –a name # to take array input echo “${name} ${name} ${name}” Expression To add integer numbers Val=`expr $a + $b` Val1=`expr $a + 100` To multiply integer numbers Val=`expr $a \* $b` To add integer/floating numbers Val=`echo $a + $b | bc -l` # -l means no. of digits after points (default 20 digits) # bc means basic calculator If Statements if [ ] then fi Example if [ $val -gt 100 ] then echo “That\'s a large number” pwd # directory fi date # date Contd… Operator Description ! EXPRESSION The EXPRESSION is false. The length of STRING is greater than -n STRING zero. The lengh of STRING is zero (ie it is -z STRING empty). STRING1 = STRING2 STRING1 is equal to STRING2 STRING1 != STRING2 STRING1 is not equal to STRING2 INTEGER1 is numerically equal to INTEGER1 -eq INTEGER2 INTEGER2 INTEGER1 is numerically greater than INTEGER1 -gt INTEGER2 INTEGER2 INTEGER1 is numerically less than INTEGER1 -lt INTEGER2 INTEGER2 Contd… -d FILE FILE exists and is a directory. -e FILE FILE exists. FILE exists and the read permission is -r FILE granted. FILE exists and it's size is greater than -s FILE zero (ie. it is not empty). FILE exists and the write permission is -w FILE granted. FILE exists and the execute permission -x FILE is granted. Nested If statements Example if [ $val -lt 100 ] then echo “That\'s a small number” if [ [ `expr $val % 2` ] -eq 0 ] then echo “Even number” fi fi If Else if [ ] then else fi Example if [ $val -lt 100 ] then echo “That\'s a small number” else echo “That\'s a large number” fi If Elif Else if [ ] then elif [ ] then else fi Example If Elif Else if [ $val -gt 18 ] then echo “You are a voter” elif [ $val -eq 17 ] then echo “You may enroll \for a voter” else echo “You are not eligible” fi Boolean Operations and (&&) / -a or (||) / -o Example if [ $val -lt 25 ] && [ $val -gt 20 ] then echo “You are eligible” else echo “not eligible” fi Assignments 1. Read the no. from user and display the sum. 2. Find the greatest no. from three no.s provided by the user. 3. Check a no. is even or odd. 4. Take user input as mark for 5 subjects and display the Avg. mark. 5. Swapping the value of two variables. 6. Find 7. Find area of a circle Loop The while loop The for loop The until loop For loop for i in 1 2 3 4 5 or # for i in {1..5} do echo –n "$i ” # -n means no newline in output done For loop Bash v4.0+ has inbuilt support {START..END..INCREMENT} syntax for i in {0..10..2} do echo "Welcome $i times" done Display all the files starting with.bash and available in your home for FILE in $HOME/.bash* do echo $FILE done For loop Syntax like c program for (( initializer; condition; step )) do shell_COMMANDS done Example: Read x for (( c=1; c

Use Quizgecko on...
Browser
Browser