Richard's Linux Ch 8
=================
Linux is case sensitive
=================
Ch 8. The Bourne Again Shell
The BASH and TCSH are command interpreters and high-level programming languages (shell scripts).
A shell script is a file that holds commands that the shell can execute.
Login Shells
/etc/profile ----- setup by user with root priviledge
~/.bash_profile ---- in your home directory, when login, use to set PATH
~/.bash_login
~/.profile
~/.bash_logout ----- when logout
Interactive Nonlogin Shells
~/.bashrc
Writing a simple Shell script
Example: create a file whoson
$ cat whoson
date
echo "Users Currently Logged In"
who
$ ./whoson
-bash: ./whoson: Permission denied
$ chmod u+x whoson
$ ls -l
-rwxr--r-- 1 leel faculty 42 Sep 7 21:38 whoson
$./whoson --------- it works
$ whoson
-bash: whoson: command not found
Why? The shell is not setup to search for files in the working directory. Need to change PATH.
#! Specifies a shell
By default, the OS try to execute a file using exec. #! helps to specify to use shell
$ cat bash_script
#! /bin/bash
echo "This is a BASH shell script"
set person = Leon
echo "person is $person"
ps -f #display the name of shell running the script
$ chmod u+x bash_script
$ ./bash_script
A command on the command line causes the shell to fork a new process, creating a duplicate of the shell process (a subshell).
$ cat nowhoson ------------- nowhoson is not executable
date
echo "Users Currently Logged In"
who
$ ls -l
-rw-r--r-- 1 leel faculty 42 Sep 7 22:22 nowhoson
$ bash nowhoson ----------------- bash creates a new shell
# Begins a Comment
Seperating and grouping comments
; and NEWLINE seperate commands
$ echo aaa; ls
| and & seperate commands and do something else
$ ls -l | grep memo | less ------------- piple, q to quit less
$ d & e & f --------------executes d and e in background and f in foreground
jobs : list jobs
$
jobs
$ sleep 60 & -------------- run in background
$ jobs
Manipulating the directory stack
Bash shell allows to store a list of directories. This list is a stack. FIFO.
$ dirs ---------------------- displays working directory
~
$ mkdir demo
$ cd literature
$ dirs
~/literature
$ pushd ../demo ------------ push directory (argument), change dir to specified directory
~/demo ~/literature
$ pwd
/home/leel/demo
$ pushd ------------------- pushd without argument, swaps the top two directories, makes the new top dir the new working directory, can use it to move to/from 2 directories, like cd
~/literature ~/demo
$ pwd
/home/leel/literature
$ pushd ../myshell
~/myshell ~/literature ~/demo
$ pwd
/home/leel/myshell
$ pushd +2 ---------------- pushd with numeric argument (+n), access other directories in stack
~/demo ~/myshell ~/literature
$ popd --------------- remove a directory from stack
~/myshell ~/literature
$ pwd
/home/leel/myshell
$ pushd ../demo
~/demo ~/myshell ~/literature
$ popd +1 --------------- remove another directory
~/demo ~/literature
Parameters and Variables
$ myvar=abc ----------no whitespace on either side of = sign
$ echo $myvar
abc
Bash shell variable is local to the command shell, the varialbe is only accessible from the program the command runs
$ cat my_script
echo $TEMPDIR
$ chmod u+x my_script
$ TEMPDIR=/home/leel/demo ./my_script
/home/leel/demo
$ echo $TEMPDIR
$
User-created variables
$ person=sam
$ echo person ----------- not a variable
person
$ echo $person ---------- variable
sam
$ person="max and zach" --------variable containing spaces
$ echo $person
max and zach
$ person="max and zach" ---------- assuming there are multiple spaces
$ echo $person
max and zach
$ echo "$person" ------------ preserve multiple spaces
max and zach
$ ls memo*
memo memo10 memo3 memo9 memo.txt
$ memovar=memo*
$ echo "$memovar" ------ "" prevent bash from performing pathname expansion
memo*
$ echo $memovar --------- shell expands the value of the variable
memo memo10 memo3 memo9 memo.txt
$ person=max
$ echo $person
max
$ person= ---------------- remove the value but not the variable
$ echo $person
$
$ unset person ----------- remove the variable
$ person=zach
$ echo $person
zach
$ person=max
$ echo $person
max
$ readonly person -------- make the variable read only (must have value first)
$ person=helen ----------------------- cannot overwrite
-bash: person: readonly variable
$ unset person ------------------------ cannot unset, must kill the shell (normally logout / exit should do it)
-bash: unset: person: cannot unset: readonly variable
Assign attributes to variables
-a : array
-f : function name
-i : integer
-r : readonly
-x : export a variable
- make it available to all subshells
$ declare person1=max
$ declare -r person2=zach_readonly
$ declare -rx person3=helen_readexport
$ declare -x person4 ----------------------------- make it available to all subshells
$ declare +x person3 ------------------ remove attribute with a +
$ declare -r ---------------------displays a list of readonly shell variables
Home directory
home directory is the working directory when login in, your home directory is stored in the /etc/passwd file
$ cat /etc/passwd
................... ---- long output here
$ grep leel /etc/passwd
................... ---- shows home directory here
$ pwd
/home/leel
$ cd demo
$ pwd
/home/leel/demo
$ echo $HOME
/home/leel
$ cd ----------------------- cd without argument, makes the direcotry stored in $HOME the working directory
$ pwd
/home/leel
$ echo ~ ----------------- ~ is like a shorthand for $HOME
/home/leel
$ ls ~/
.................. ------------ all files in HOME
$ echo $PATH ------------ PATH variable specifies the directories in the order the shell should search them
exporting PATH makes it value accessible to subshells
$ export PATH=/usr/local/bin:/bin:/usr/bin;~/bin: ----- don't need to try this
$ PATH=$PATH:/usr/mydir ------ add another directory to the end of the list of directories
User Prompt ---- skipping
A process is the execution of a command by the Linux kernel. The shell that starts when you log in is a command, or a process, like any other.
Give linux utility name on the command line => process
Run a shell script => process
Process structure is hierachical. A parent process forks a child process, which in turn can fork other processes.
$ sleep 10 & ------ remember the PID
$ sleep 10 &
$ pstree -p ------------- show the huge ps tree, find the forked PIDS
$ history -------------- display events in the history list
$ history | tail
Reececuting and editing commands ----- skipping