Shell Part 2

advertisement
Shell
Part 2
Sarah Diesburg
Operating Systems
CS 3430
1
Environmental Variables

Gives programs specific information about your
environemnt, such as your execution paths
sarah@trogdor:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games

May be set by you or other shell scripts (like .bashrc)
sarah@trogdor:~$ export TEST=hello
sarah@trogdor:~$ echo $TEST
hello
2
Environmental Variables
char *getenv(const char *name);

Returns value of an environmental variable,
NULL if not found

Overwrites current variable info with value
char *setenv(const char *name,
const char *value,
int overwrite);
3
Environmental Variables

Important examples





$PATH
$USER
$PWD
Many shell prompts incorporate these
The command ‘pwd’ also uses $PWD
4
Resolving Pathnames?

Sometimes we don’t pass the full command
name to the shell



E.g. pass ‘ls’ instead of ‘/bin/ls’
Something has to translate ‘ls’ into ‘/bin/ls’
Execvp searches all of the user’s paths
stored in the $PATH environmental variable
5
Finding full pathname for ‘ls’
$PATH=/usr/local/bin:/usr/bin:/bin

Does /usr/local/bin/ls exist?


Does /usr/bin/ls exist?


No
No
Does /bin/ls exist?

Yes!
6
$PATH

What happens when we add a “:.” to the end
of $PATH?
7
cd

We are very used to using cd, but it is not a
real command.


Try ‘which cd’ at the prompt – there isn’t one!
The command cd is actually a built-in
command of the shell
8
cd
int chdir (const char *path)


Changes the working directory
Also need to update the $PWD so that it
matches


Otherwise the pwd command will be strange
Use setenv() to update $PWD
9
cd Behavior

cd with no arguments


cd with ‘.’


PWD does not change.
cd with ‘..’


Reverts the present working directory to $HOME.
PWD changes to the parent.
cd <dir>


If DIR is found and DIR is a directory, change the PWD to DIR
If not, signal that DIR does not exist
10
cd Implementation Hints

Look at the following:





setenv
getcwd
chdir
pwd
Another hint: use the man pages to see what
they do

$> man getcwd
11
Redirection

Redirection of stdin and stdout happen by
placing a redirection character between
commands



[command] < [in_file]
[command] > [out_file]
[command] < [in_file] > [out_file]
12
Input Redirection



[command] < [in_file]
The command now takes input from in_file
instead of stdin
Examples


cat < file1.txt
grep hamburger < menu.txt
13
Output Redirection



[command] > [out_file]
Prints the output from command to out_file
instead of stdout
Example

echo hello > file1.txt
14
Combination Redirection



[command] < [in_file] > [out_file]
The command takes input from the in_file
instead of stdin and prints output to the
out_file instead of stdout
Example

cat < file1.txt > duplicate.txt
15
Pipes
One program can receive output from
another without an explicit temporary file
command1 | command 2

Same as
command1 > tmpfile
command2 < tmpfile
rm tmpfile

16
Pipe Example

$> ps | grep bash


$> ps aux | grep bash



Finds your bash process
Finds everyone’s bash process
So what do we think grep does?
How can we find all processes owned by us?
17
Pipe vs Redirection

So, when would we want to use a redirection
over a pipe?

Pipe over redirection?
18
Download