between the user commands and the kernel Multiple shells are available in Linux $ cat /etc/shells $ chsh Only Bash (Bourne Again SHell) gives access to 100% features of Linux Wildcards Wildcards are used to match muliple files/folders * matches zero or more characters ? matches any single character [a-z] matches a range of characters [^a-z] matches all except the range Special characters ~ tilde character represents current users home directory ls /home/student/myfolder ls ~/myfolder ` back ticks echo Today's date is `date` Variables Local variables and environmental variables Local: Variables created will remain in the current shell Environment: A copy of the variables created will be passed to child shells set (display all variables local+environment) env (display only environment variables) Create vars $ A=10 $ B=20 $ export B Bash startup scripts Login shell : Shell that is started after a login action Non-login shell: Shell that is started without a login action Login shell /etc/profile /etc/profile.d/ ~/.bash_profile ~/.bashrc /etc/bashrc Non-login shell ~/.bashrc /etc/bashrc /etc/profile.d/ Logout ~/.bash_logout Simple bash script $ vi script1.sh
#!/bin/bash # This is a bash script comment.
echo hello world
$ bash script1.sh
$ chmod +x script1.sh $ ./script1.sh sha-bang character Path to shell(interpreter) comment cont... #!/bin/bash
echo My PID number is $$ echo -n Enter first number: read A printf Enter second number: read B
echo The total is echo $A+$B | bc -l
C=`expr $A + $B` printf Total is %d $C
Internal variables $BASH_VERSION The version of Bash installed on the system $FUNCNAME Name of the current function $GROUPS Groups current user belongs to $HOME Home directory of the user, usually /home/username $HOSTNAME The system host name $PATH Path to binaries/commands $PPID The $PPID of a process is the process ID (pid) of its parent process. $PS1 This is the main prompt, seen at the command-line.
arithmetic operators + plus - minus * multiplication / division ** exponentiation
% modulo, or mod (returns the remainder of an integer division operation)
cont... #!/bin/bash
read -p Enter marks for eng, sci, math M1 M2 M3 if [ -z $M1 -o -z $M2 -o -z $M3 ]; then echo enter 3 values exit 1 fi
let TOTAL=$M1+M2+M3 let AVG=$TOTAL/3 If [ $AVG -gt 90 ]; then echo Merit elif [ $AVG -gt 80]; then echo First class else echo Avg is $AVG fi
exit 0 # exit status with $? Integer operators -eq is equal to -ne is not equal to -gt is greater than -ge is greater than or equal to -lt is less than -le is less than or equal to
String operators -n STRING the length of STRING is nonzero
-z STRING the length of STRING is zero
STRING1 = STRING2 the strings are equal
STRING1 != STRING2 the strings are not equal
cont... #!/bin/bash
read -p Enter file name: FILENAME
if [ ! -f $FILENAME ]; then echo enter filename exit 1 Fi
case $FILENAME in *.conf) echo Seems like a configuration file ;; *.log) echo Seems like a log file;; *) echo Cant' guess the file type ease cont... #!/bin/bash
for i in 1 2 3 4 5 6 do echo $i done
for i in {1..10} do echo $i done
for i in $(seq 1 10) do echo $i done cont... #!/bin/bash
A=10 while [ $A -lt 10 ] do Echo $A Let A=$A+1 done #!/bin/bash FILES="/usr/sbin/accept /usr/sbin/pwck /usr/sbin/chroot /sbin/ypbind"
for file in $FILES do if [ ! -e "$file" ]; then echo "$file does not exist."; echo continue elif [ -d $file ]; then echo Directory found break fi ls -l $file | awk '{ print "file size: " $5 }' done exit 0
File test operators -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -L FILE FILE exists and is a symbolic link -r FILE FILE exists and read permission is granted -s FILE FILE exists and has a size greater than zero -w FILE FILE exists and write permission is granted -x FILE FILE exists and execute permission is granted
cont... #!/bin/bash
If [ $# -lt 2 ]; then echo Usage: $0 filename1 filename2 ... fi
expr index $string $substring Numerical position in $string of first character in $substring that matches.
${string:position:length} Extracts $length characters of substring from $string at $position.
expr substr $string $position $length Extracts $length characters from $string starting at $position.
${string#substring} Deletes shortest match of $substring from front of $string. ${string##substring} Deletes longest match of $substring from front of $string. ${string%substring} Deletes shortest match of $substring from back of $string.
cont... ${string/substring/replacement} Replace first match of $substring with $replacement. [40] ${string//substring/replacement} Replace all matches of $substring with $replacement.