keyword shell variables the shell sets keyword shell variables. you can use (and change) them. home...

8
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where the shell looks for executables USER Your login name SHELL The name of the shell you are running PWD The current working directory PRINTER Can be loaded with your default printer

Upload: ernest-garrett

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

Keyword Shell Variables • The shell sets keyword shell variables.

You can use (and change) them.

HOME The path to your home directory

PATH Directories where the shell looks for executables

USER Your login name

SHELL The name of the shell you are running

PWD The current working directory

PRINTER Can be loaded with your default printer

Page 2: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

$ cat env#!/bin/shecho "Hi $USER!"echo "Your home is: $HOME"echo "Your path is: $PATH"echo "Your current directory is: $PWD"echo "Your shell is: $SHELL"echo "Your printer is: $PRINTER"$ envHi qyang!Your home is: /homes/qyangYour path is:/usr/bin:.:.:/homes/horner/Unix/bin:...Your current directory is: /homes/qyang/111Your shell is: /bin/cshYour printer is: csl3

Page 3: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

Read only Shell Variables • Read Only means you cannot change it; just read its

value• $0 is the program name (the filename the user typed

to invoke the shell script):

$ cat print1#!/bin/shecho "This script is called $0"$ print1

This script is called print1$ ./print1This script is called ./print1$ ~/111/print1This script is called /homes/qyang/111/print1

Page 4: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

Command Line Arguments – (1)

• The command line arguments that you call a script with are stored in variables $1, $2, ..., $9.

$ cat args1#!/bin/shecho "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9"$ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

The args are a1 a2 a3 a4 a5 a6 a7 a8 a9

• With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.

Page 5: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

Command Line Arguments – (2) • Example: How to write a command to swap two

files?$ cat swap#!/bin/shmv $1 /tmp/$1mv $2 $1mv /tmp/$1 $2

$ cat it1contents of file1$ cat it2contents of file2$ swap it1 it2$ cat it1contents of file2$ cat it2contents of file1$

Page 6: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

Command Line Arguments – (3)

• $* lists all the command line args:$ cat args2#!/bin/shecho "The args are $*"$ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

• $# contains the number of args:$ cat args3#!/bin/shecho "The number of args is $#"$ args3 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10

The number of args is 10

Page 7: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

• The for statement executes a loop once for each of a list of possibilities:

$ cat printall#!/bin/shfor file in *do

if [ -f $file ]then

echo "Print $file [y/n]? "read respif [ $resp = "y" ]then

lpr -Pcll3 $filefi

fidone

$ printallPrint letter1 [y/n]?yPrint names [y/n]?n

Looping using for – Example 1

Page 8: Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where

• The in clause of the for statement accepts as many parameters as you wish in many forms:

$ for i in 1 2 3 ; do echo $i ; done123

$ for pid in `ps -a | tail +2 | cut -c1-6 | sort`> do> kill -9 $pid> donekill: permission deniedkill: permission deniedkill: permission denied

(you will then be logout!)