Chapter 2:Shell Programming

来源:互联网 发布:网络英语外教哪家好 编辑:程序博客网 时间:2024/06/06 03:28

Numerous examples of shell scripts are already loaded on your Linux system in case you’re curious,

including package installers, .xinitrc and startx, and the scripts in /etc/rc.d to configure the

system on boot-up.

 

On Linux, the standard shell that is always installed as /bin/sh is called bash (the GNU Bourne-Again SHell)

 

On most Linux distributions,the program /bin/sh,the default shell,is actually a link to the program /bin/bash

so /bin/bash <=> /bin/sh

 

-->Pipes and Redirection

file descriptor 0:input

file descriptor 1:output

file descriptor 2:error output

 

-->Redirecting Output

overwritten

$ls -l > lsoutput.txt

append

$ps >> lsoutput.txt

=======================================================================================

prevent the kill command from writing any text to the screen

$kill -HUP 1234 >killout.txt 2>killerr.txt

combine the two outputs to capture both sets of output into a single file

$kill -l 1234 >killouterr.txt 2>&1

not printf on the serial port,display on the background

$kill -l 1234 >/dev/null 2>&1

=======================================================================================

-->Redirecting Input

$less < killout.txt

 

-->Pipes

$ ps > psout.txt

$ sort psout.txt > pssort.out <=>$ ps | sort > pssort.out

$ ps | sort | less

=======================================================================================

to see all the different process names that are running excluding shells

$ ps –xo comm | sort | uniq | grep -v sh | more

This takes the output of ps, sorts it into alphabetical order, extracts processes using uniq, uses grep -v sh to remove the process named sh, and finally displays it paginated on the screen.

 

wary:never use the same file-name twice in a string of commands

$cat mydata.txt | sort | uniq > mydata.txt

it will end up with an empty file, because you will overwrite the mydata.txt file before you read it

 

-->Shell Programming

There are two ways of writing shell programs:

1.type a sequence of commands and allow the shell to execute them interactively

2.store those commands in a file that you can then invoke as a programme

Interactive Programs

--to find a file and display which contains POSIX

=======================================================================================

$ for file in *

> do

> if grep -l POSIX $file

> then

> more $file

> fi

> done

=======================================================================================

list the files my_fingers and my_toes

$ ls my_{finger,toe}s

output the name of the file whose contents contained the string POSIX

$ less `grep -l POSIX *`

$ less $(grep -l POSIX *)

$ grep -l POSIX * | less

 

 

-->Creating a Script

"#"    is comments start and continue to the end of a line

"#!"   tell the system that the programme to be used to execute this file

"exit" command ensures that the script returns a sensible exit code

"0"    denotes success in shell programming

=======================================================================================

#!/bin/sh

# first

# This file looks through all the files in the current

# directory for the string POSIX, and then prints the names of

# those files to the standard output.

 

for file in *

do

         if grep -q POSIX $file

         then

                   echo $file

         fi

done

 

exit 0

=======================================================================================

-->Making a Script Executable

run your script in two ways:

1.invoke the shell with the name of the script file as a parameter

$ /bin/sh first

2.changing the file mode to make the file executable for all users suing the chmod command

$ chmod +x first

execute by using the command

$first

if error saying the command can't found,set path

PATH=$PATH:.

if u want the script to be execute by others.take the script

/usr/local/bin ot another system directory as a convenient location for adding a new programme

why not 777? to prevent other from changing ur script

# cp first /usr/local/bin

# chown root /usr/local/bin/first

# chgrp root /usr/local/bin/first

# chmod 755 /usr/local/bin/first or #chmod u=rwxgo=rx /usr/local/bin/first

 

 

-->Shell Syntax

 

Variables: strings, numbers, environments, and parameters

$ salutation=Hello

$ echo $salutation

Hello

 

$ salutation=”Yes Dear”

$ echo $salutation

Yes Dear

 

$ salutation=7+5

$ echo $salutation

7+5

 

$ read salutation  --input

Wie geht’s?

$ echo $salutation --output

Wie geht’s?

 

-------please notice the function of the quotation marks

=======================================================================================

#!/bin/sh

 

myvar=”Hi there”

 

echo $myvar   --Hi there

echo “$myvar” --Hi there

echo ‘$myvar’ --$myvar

echo \$myvar  --$myvar

 

echo Enter some text

read myvar

 

echo ‘$myvar’ now equals $myvar

exit 0

=======================================================================================

$ ./variable

Hi there

Hi there

$myvar

$myvar

Enter some text

Hello World

$myvar now equals Hello World

=======================================================================================

------we can figured out from the example that $and"" is the value of the variables;''and\ is the name of the variables.

--Environment Variables

=======================================================================================

$HOME The home directory of the current user

$PATH A colon-separated list of directories to search for commands

$PS1  A command prompt, frequently $, but in bash you can use some more complex values; for example, the string [\u@\h \W]$ is a popular default that tells you the user, machine name, and current directory, as well as providing a $ prompt.

$PS2  A secondary prompt, used when prompting for additional input; usually >.

$IFS  An input field separator. This is a list of characters that are used to separate words when the shell is reading input, usually space, tab, and newline characters.

$0    The name of the shell script

$#    The number of parameters passed

$$    The process ID of the shell script, often used inside a script for generating unique temporary filenames; for example /tmp/tmpfile_$$

=======================================================================================

--Parameter Variables

=======================================================================================

Parameter    Variable Description

$1, $2, …    The parameters given to the script

$*           A list of all the parameters, in a single variable, separated by the first character in the environment variable IFS. If IFS is modified, then the way $* separates the command line into parameters will change.

$@           A subtle variation on $*; it doesn’t use the IFS environment variable,so parameters are not run together even if IFS is empty.

=======================================================================================

$ IFS=’‘

$ set foo bar bam

$ echo “$@“

foo bar bam

$ echo “$*“

foobarbam

$ unset IFS

$ echo “$*“

foo bar bam

=======================================================================================

#!/bin/sh

#try_var

salutation=”Hello”

echo $salutation                           --Hello

echo “The program $0 is now running”       --The program ./try_var is now running

echo “The second parameter was $2”         --The second parameter was bar

echo “The first parameter was $1”          --The first parameter was foo

echo “The parameter list was $*“           --The parameter list was foo bar baz

echo “The user’s home directory is $HOME”  --The user’s home directory is /home/rick

echo “Please enter a new greeting”        

read salutation                            --Sire

echo $salutation                           --Sire

echo “The script is now complete”

exit 0

=======================================================================================

$ ./try_var foo bar baz

Hello

The program ./try_var is now running

The second parameter was bar

The first parameter was foo

The parameter list was foo bar baz

The user’s home directory is /home/rick

Please enter a new greeting

Sire

Sire

The script is now complete

$

=======================================================================================

Conditions: shell Booleans

Program control: if, elif, for, while, until, case

Lists

Functions

Commands built into the shell

Getting the result of a command

Here documents

原创粉丝点击