Week 2 Lecture 3 OPS435
The handout is the page tells you the 9 steps the shell tells, you interprets
3. ~
4. ‘ ‘ $()
5. $
6. * ? []
7. ‘ ‘ “ “ \
8. < > >>
Once you go through steps 1-8 the shell has interpreted the command and executes it 80% kernel, 20% shell, if its built in then its already in the shell, if its not a built in command the shell has to pass it on to the kernel before the kernel executes the command it has to copy it from the database to memory, the kernel does not know where it is in the database, that’s another function of the shell, it must tell the kernel where to it is in the database, How does it know….path variable
Lets say where doing the ls command, it must go into the $PATH variable and its going to go to the first variable and sees if its their and goes to the next and when it find it it passes the information to the kernel, then the kernel will go and copy the command or utility from the database to the shell and assign it a unique PID number, it will go back into memory and release the command then the kernel goes and nudges and shell and says its done and asks what do you want me to do next, to finish this one off we have a command
Mail –s “$host System Maintenance Warning!” ‘cat ~/userlist*’ < maint01
There are two commands, command substitution ‘ ‘, the car command will be executed before the mail command is executed
As soon as the command their it executes Token Splitting, then it looks inside the ‘ ‘ and find the tild which is 3, it will take the tild character and replace it with the home directory, then it goes to number 6 because of the star it is looking at the ‘cat ~/userlist*’ it will go into the home directory and look at any file starting with the name user list, userlist* will be replaced will all the files with the names matched, the cat command read the contents of the files, so the whole cat command will be replaced with the STDOUT of that command and will be replaced from ‘cat ~/userlist*’ this and will send the content of the directory of all the user names, so when this is done the shell will continue to 5 and down, so were looking at the $host and will replace the $host with matrix, 6 no, 7 quotes yes the quotes are their, the quotes will be removed, 8 I/O Redirection yes < STDIN which will open the file read the contents and pass the contents to the command, during step 8 it will open the FILE, all the interpretation is done when all of its done, in step 9, after step 9 it will pass it to the kernel, it will go to the path variable to find where the mail command is located, once its done the shell jobs is done and the kernel will take over from their.
> clears the file and prepares it for receiving data,
1,4(1,3,6,9) 5,7,8,9
Another example when the shell does not go through the path variable, 1 when the command is built in 2 the mail command, instead of the mail command you put in /bin/mail…will the shell go to the path variable…no because we have already specified where the path is so the shell does not have to find it because this is absolute addressing. The shell is about different characters, that is how you ask the shell to do things, if you don’t know the special characters you do not speak the shell’s language.
Covering a couple of commands you are going to need a number of commands for Lab 1, Lets start off with the sort command, sort command not a easy command, and is very complex if you make use of all the capabilities, lets go through the command, there are two different sort command the old and the new,
Sort sorts the contents of the file and puts them in an order, what order, it uses ASCII code for its default code for sorting everything you may use many option to change what it is to do, but remember its done automatically by ASCII code, both new and old work that way.
Capital A 101 comes before small a 141, Numbers come before Capitals
With the sort command you can specify, lets do this
sort f1
if you simply do this command will start sorting from what character, the first character, most files are split in fields, you can specify which field you want, it automatically goes to field one though,
The difference between old and new how you specify how u want to do this
The old is SKIP SORT the programmers that divised there is no other reason, the reason its called this, say you want to start on field number 2 instead of says start on field 2, the skip sort asks what field do you want to skip to start on so
Sort +1 f1
+ means skip 1 and this command will start on field 2
With the sort command you say start on this one and stop on this one, multiple fields of starting and stopping lets say you wanna start on field two and stop at four
Sort +1 -4 f1
Lets say you want to continue
Sort +1 -4 +5 -9
On 5 and stop at 9, what field are we going to do its ASCII
6 through 9 are numbers, if you wanan sort numerically you have to tell the command you have to use –n so if +5 -9 is only numeric you put the n here
Sort +1 -4d +5 -9n
The d is for dictionary field, that is basically how it works, now you can also specify….not important….how to start on a certain character field #5 and char #6 you can be very precise, you can look that up in your man pages if you wish. Particular char then a field, so that the skip sort command the new skip command is called key word sort the only difference is how to specify the fields you want to stop and start on, it keys exactly to the field you want to go to
Sort –k
As soon as k keyword sort command, so we want to do 2 4 so.,,
Sort –k2,4d –k6,9n
It is the same as
Sort +1 -4d +5 –9n
We are going to look at some other options on this command, the default delimiter is white space, tab and new line this is called white space,
Now what is the delimiter in the file /etc/passwd its semicolon, what if you want to sort in a file with semicolon so you have to have the option to change the delimiter
Sort –t: that will specify what type of delimiter it is.
Sort /etc/passwd
Sort –t: -k3n /etc/passwd
Or
Sort –t: +2n /etc/passwd
-f force case insensitive, don’t differentiate between upper case and lower case you use –f
-r sort in reverse
Lets try another command cut, what is the purpose, you can cut data from a file, you may cut portions for any particular purpose, so the cut command you specify what is it you want to cut
Cut f1
The type of data it was designed to used the default delimiter was a tab, it is not white space but a tab, so any time you use it you should know the default delimiter was a tab, if its not a tab you are going to have to change the delimiter what is the delimiter change for the cut command
Cut –d: f1
You cut either fields or characters with this command and the way you do this, there is another option in their which is cutting fields
Cut –d: -f3 f1
Three through 5
Cut –d: -f3-5 f1
Three through 5 & 7
Cut –d: -f3-5,7 f1
Range and the command says and this
Also –c characters you can cut characters so you can say
Cut –c10-20,25,28
And that’s the cut command, another command
Paste
One easy way to remember how it works, everyone knows how cat works
Cat 1 2
Its going to append the two together vertically, the paste command works similar, instead of vertically it appends them horizontally, its going to put a delimiter which is a tab between the two
Paste –d+ banana
Instead of the tab it will have plus signs.
Paste –s
Which means squeeze its going to take the whole file and squeeze all the files into one single line. Single file only.
Remember all the commands you are using it does not do anything to the file, the original data remains as it was.
Here we are going to try something, assign a variable age you year of birth in this format
YYYMMDD
$age = 19871223
Echo $age |cut –c1-4
1987
Echo “The year of my birth is $(echo $age | cut –c1-4)”
The year of my birth is 1987
Echo “The month of my birth is $(echo $age |cut –c5-6)”
The year of my birth is 12
The shell basics with commands, okay Chapter 3.
Login Files, when you log onto the system there are a couple of files that are automatically excuted when you log in the shell environment is predetermined by these three files
/etc/profile – this sets the environment for every user, same file to every user, which belongs to root, and super user can use this to set up the file for every user
Two other files
~/.bash_profile – the user is in charge of this, if you want to change your environment you would go into this file, if you want ur prompt to look different you can go into this file, if you want to create a new command you can put alias command and when you log in it will stay their.
~/.bashrc – this belongs to the user as well, and you can go to either one and make changes to you environment.
~/.bash_logout - when you log out , want something done when you log out u do this
Open up ~/.bash_profile
cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
BASH_ENV=$HOME/.bashrc
USERNAME=""
export USERNAME BASH_ENV PATH
PS1="\[\033[21;255;44m\u@\033[21;255;44m\h:\033[1;255;41m\w$\033[0m\] "
LS_OPTIONS='-N --color=tty -T 0'
Cd what does this command do, takes you to your come directory
Cd - - takes you back to where you were before
Pwd
Random – echo $RANDOM you get a random number, if using a program and you want to generate random numbers that is how your going to do it
Shell Built Commands are on the slides….
Special operators …first semi colon what does that mean to the shell?
Cd
echo
Cat
Sort
Writing a program so on every line lets say we want to do all the commands all the same way but we want it on one line
Cd ; echo; cat ; sort
; - command separator
& - used for
Sleep 10 – shell sleep for 10 seconds
Sleep 10 &
[5] 15962
Job #5 and is running in the background and gives you the environment to play with still, it will assign the job number and run in the background, if you hit the return it wills say job #1 is done.
The labs are normally due the date, but we going to do if u don’t finish on Friday up to next Friday.
No comments:
Post a Comment