A shell is an interpreted environment. This means you can enter commands and upon hitting the enter key, those commands are interpreted.
An interpreted program is translated and executed line by line, whereas the more traditional compiled program is translated entirely first and then executed.
The Linux/Unix shell is tailorable.
Interacting at the shell level not only provides a greater degree of flexibility and control over the OS, it also saves resources because the shell itself does not require, for example, complicated graphics commands or mouse tracking.
The user can open any number of terminal windows–each is its shell, its own interpreted environment.
One of the earliest Unix shells was called the Bourne Shell (sh)(1977), then came later shells like the C shell (csh), the T shell (tcsh), the Korn shell (ksh) and the Almquist shell(ash).
Bash has many of the same features from the original Bourne shell, but adds features that were created for such shells such as csh and ksh. It also simplifies some of th esyntax required in the Bourne shell so that, although it is a larger shell, it is easier to use.
Several features that make entering and editing commands easier
History
Command-line editing
Tab completion
Aliases
Brace expansion
Tilde expansion
Wildcards
Every time you enter a command in your current Bash session, it is recorded in what is called the history list. To view history list, type the command history.
Control+p→ retrieves the previous instruction, cntrl+n(next)
Another way to recall an instruction from the history list is by typing !number, where number is the number of the entry in the history list (exclamation point=bang)
You can use !chars where chars are the first characters of the command you wish to repeat.
More useful commands:
control+n=display the next command from the history list on the command line
control+p=display the previous command from the history list on the command line
control+f=move cursor forward one character
control+b=moe cursor backward one character
Escape key+f=move cursor forward one word
escape key+b=move cursor backward one word
control+a=move cursor to the beginning of the line
control+e=move cursor to the end of the line
control+d= delete the character where the cursor is at
control+k= kill(delete) the contents from the cursor to the end of the line
control+w=delete all the characters before the cursor to the beginning of the word
control+y=take whatever was recently deleted and yank it back put it in current cursor position
control+_= undo the last editing operation (delete or paste)
Another very useful bash editing feature is called tab completion. As you enter a directory name or filename, if you reach a portion of that name that is unique, pressing the tab key will cause the interpreter to complete the name for you.
You want to view one of these files using less* so less fo <tab>
Pressing two tab keys will cause the Bash interpreter to list all matching filenames less fr <tab> <tab>
In Linux, an alias is a shortcut command. You define an alias so that when you type in your alias, it is replaced by the full command.
alias name=command
alias add//usr/sbin/useradd
You define aliases to simplify typing.
Reduce the length of an instruction that contains options
alias ls=’ls-l’
Interesting ideas for aliases
alias . . = ‘cd . .’
alias . . .=’cd . ./ . .’
alias md=mkdir
If you define an alias at the command line, the alias exists only in that shell session, it would not exist in other terminal windows, nor if you started a new shell session.
Tilde expansion ~ is used to signify the user’s home directory, and also for other user’s home directories with ~username.
The brace expansion is used when you have a list of items that a command should be applied to {} each item is separated by a command.
ls {foo1,foo2,foo2/{foo2a,foo2b},foo3}
Filename expansion- this takes place when you use wildcards among your filenames
A wildcard can act as anything, * “match anything”
ls *txt would list all files that end with a .txt extension no matter their name.
A variable is a name given to a storage location, by using a variable you can reference that variable from various commands rather than having to use the value directly.
2 types of bash variables:
User variables are those defined by the user and often only used via command-line operations
Environment variables are established either by the OS, the Bash interpreter or by some other running software.
To see what environment variables are set, enter the command env.
All the environment variables are capitalized.
variable=value, assignment statement on the command line to establish a variable.
Bash will treat any number as a string unless you specify that it should be a number age=(29)
To obtain the value stored in a variable, you must precede the variable name with a $
first=Frank last=zappa. fullname=”$first $last”
If the value in the variable is a number and you want to perform some type of arithmetic operation on it, you have to enclose the entire operation in $(( … ))
$((age+1))
Reassignment - age=$((age+1))
The arithmetic operations available are +, -, * (multiplication_, / (division), % (remainder/modulo)
You can also view the contents of variables using the echo command. The echo command expects a list of items
echo Hi there, how are you? <- literally prints this line
echo $fullname
echo statement does not require that you place the list of items in quote marks
Linux has the command date
echo “Hello $fullname, today is date
” / “Hello $fullname, today is $(date)”
PATH variable stores a list of directories, whenever you enter a Linux command or file name, if the item is not found in the current working directory, then the Linux interpreter checks for the item in every directory listed in the PATH variable.
If you want to add to your PATH variable, you could issue a reassignment statement as follows: PATH=$PATH:newdirectory and or PATH=$PATH:/home/foxr <- gets added to our path
Redirections
< redirect the input to come from an input file
> redirect the output to go to an output file, overwriting the file if it already exists.
>> redirect the output to be appended to an already existing file, or create the file if it does not exist
<< redirect the input to come from keyboard where input will terminate with a special keyword that you specify after <<
| redirect the output of one command to be the input of another- this is known as pipe
cat foo1.txt > foo2.txt - this instruction might give you the same result as if you did cp foo1.txt foo2.txt. However, imagine that you wanted tot ake the output of several files and copy them to another file, the cp command doesn’t do this.
cat file1.txt file2.txt file3.txt > file4.txt.
File4 would then be the concatenation of file1.txt, and file3.txt.
The << is of use when you want to force a program that accepts its input from file to accept your keyboard input instead.
After the << you place a keyword that will terminate the input.
cat << done
Now to redirect the input to an output file
cat << done > shoppinglist.txt , whatever we have typed is now stored in the file shoppinglist.txt.
If you want to save file information
ls -l >> directory_listing.txt
The pipe redirects the output of one program to be the input of another program.
THe most common form of help is the manual pages (man) for an instruction, just type man.
Includes : syntax of its usage, description of the instruction, the options/parameters available for the instruction, the authors of the program, files related ot the command and other commands related to or similar to this one.
find command is similar but includes a list of examples to help illustrate how to use the command.
help commandname = syntax for help command, but very few Linux commands have help pages so it is best to use man
Apropos allows us to search for a command based on what it accomplishes/does. You would do apropos string and then it’ll list all the commands that have the associated string found in the command’s description.
apropos “virtual memory”
As a Linux User, you will often use a text editor instead of a word processor.
This is certainly true of system administrator who will often have to edit configuration files or write shell script files, all of which should be stored as normal text files.
3 common text editors:
GUI editor : Text Editor
vi
emacs
vi editor
Because it is text based the commands that you would find in menus in a GUI-based editor are performed as keystrokes
The editor uses 3 different modes: command mode, insert mode and replace mode.
Command mode is the default mode so keystrokes are interpreted as commands, a command preceded by a number means that command is performed that many times
5dd deletes five consecutive lines
Insert mode is the mode similar to a word processor, with each keypress the cursor advances to the next position. The only keystroke command in this mode is the escape key, which exits the mode returning you to command mode.
Replace mode is similar to insert mode, however as you enter keystrokes the characters overwrite the characters already present.
Mode commands
i = Enter insert mode at the immediate left of the cursor
a=Enter insert mode at the immediate right of the cursor
o=insert a blank line after the current line and enter insert mode
O=insert a blank line before the current line and enter insert mode
I= Enter insert mode at the beginning of current line
A= Enter insert mode at the end of the current line
r= Replace one character with the next character entered
R= Enter replace mode and continue to replace (overwrite) characters (until escape)
You can mark text and then jump to the marked text
Repeat and undo commands
u = undo the previous command
. = Repeat the last edit
n.= Repeat the last edit command n times
nk = Repeat command k n times
Delete,copy paste and change commands
x = delete the next character
nx = delete the next n characters
dw= delete the next word
ndw=delete the next n words
dd=delete this line
ndd= delete the next n lines
D= delete from the cursor to the end of the line
yy=copy the current line into a buffer
nyy= copy the next n lines
p= paste any lines below current lines
J= join the next line to the end of the current line
cw= change current word by replacing text as you type
C= change all words in current line until user presses esc key
File commands
:w <enter> = save the file
:w name <enter> = save the file as name
:q <enter> = exit vi
:q! <enter> = exit vi without saving
:wq <enter> = save file and exit vi
A script is an interpreted program
A script can receive parameters making it more powerful than commands typed at the command line
Shell scripts are stored in text files that must be both readable and executable
./foo = executes a script named foo
All bash shell scripts should start with the following line, which alerts the bash interpreter to execute : #!/bin/bash.
With arithmetic, the division operation (/) operates only on integers and returns an integer quotient.
Input is accomplished through the read statement, which is usually followed by a variable. It will result in the cursor bling on a blank line.
echo Enter your name
read NAME
echo Hello $NAME, nice to meet you!
We need parens, so the average division isn’t performed before the additions
#!/bin/bash
echo Please input your first number
read num1
echo Please input your second number
read num2
echo Please input your third number
read num3
echo Please input your fourth number
read num4
quotient= $ (((num1+num2+num3+num4)/4))
remainder= $ (((num1+num2+num3+num4)%4))
echo The average is $quotient with a remainder of $remainder/4
Conditional statement in bash “if [condition]; then actions(s); fi”
There must be spaces between the various components of the if statements as show
fi ends our if statement
Actions are individual statements separated by semicolons.
Two common forms of conditional statements are variable comparison value and filetest filename
The variable’s name is preceded by a $ and the comparisons are
== (string)
!- (string)
-eq (equal to)
-ne (not equal to)
-gt (greater than)
-lt (less than)
-ge (greater than or equal to)
-le (less than or equal to)
The second test is a filetest condition followed by a filename
-d (item is a directory)
-e (file exists)
-f (file exists and is a regular file)
-h (item is a symbolic link)
-r (file exists and is readable)
-w (file exists and is writable)
-x (file exists and is executable)
! is when a condition should be negated
-r foo.txt asks if foo.txt is readable whereas ! -r foo.txt asks if foo.txt is not readable
Example conditions
[ $NAME== “Frank”]
[$X -gt $Y]
[$X -eq 0]
[$STUDENT1 != $STUDENT2]
[-x $FILENAME]
[ ! -r $FILE2]
In this if-then statement, the variable $NAME is tested against the value “Frank” and if they are equal, the instruction both outputs the message “Hi frank” and adds 1 to the variable X, the fi ends the if statement.
if [ $NAME == “Frank”]; then echo Hi Frank; $X=$((X+1)); fi
Compound conditional requires more than one comparison, is combined with a Boolean AND (&&) or OR ( || ) , and requires an additional set of [ ]
[[ $X -ge 0 && $X -le 100]]
An else clause can be added, it will appear after the actions in the then clause and the fi appears after the else.
if [ $NAME== “Frank”]; then echo “Hi Frank”; X=$ ((X+1));
else echo Who?; X=0;
fi
if [$X -gt $Y]; then Y=0;
else X=0;
fi
There is also a nested if-then-else statement, where else if is writted as elif.
if [ condition ] ;
then
action(s);
elif [condition]
then
action(s);
else
action(s);
fi
The for-loop is an iterator loop that will iterate over a list
First purpose is to iterate through the items in a directory
Second is to iterate through parameters in order to process them.
for file in *.txt; do
if [ -x $file ];
then echo $file;
fi
done
// this loop iterates through all the .txt files in the current directory and displays the file names of those files that are executable.
While loop/conditional loop: You specify a condition and while that condition remains true, the loop vody is executed.
while [condition] ; do statement(s); done
To prevent infinite loop, we should ask the user a special keyword to exit such as “quit” this is known as a sentinel value
#!/bin/bash
COUNT=0
echo Enter a filename, quit to exit
read FILENAME
while [ FILENAME != quit ]; do
if [ [ -r $FILENAME && -w $FILENAME ] ];
then ls-l $FILENAME; COUNT=$((COUNT +1));
fi
done
echo There were $COUNT files that were both readable and writable.
//the following script repeatedly asks the user for file names, outputs those files if they are btoh readable and writable and exits the loop once quit is entered.
#/bin/bash
VALUE=1
while[ $VALUE -lt 1000];
do
VALUE=$((VALUE*2));
done
echo the first power of two greater than 1000 is $VALUE
//the following script computer the powers of 2 less than 1000, and outputs the first one found to be greater than or equal to 1000.
Bash scripts can pass parameters usually entered at the command line prompt before the script name
A script is executed using the notation ./script <enter> so with parameters ./script 5 10 15 <enter>
$# stores the number of parameters
$@ returns the entire list of parameters, use in a for loop as the list
./ avg 10 381 56 18 266 531
#!/bin/bash
if [ $# -eq 0 ]; then echo No parameters, cannot compute average;
else
SUM=0;
for NUMBER in $@; do
SUM=$((SUM+NUMBER));
done
AVERAGE=$((SUM/$#));
echo The average of the $# values is $AVERAGE;
fi