国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
ksh編程

Ksh Scripting


Contents

Principleof Script
Variables
Branching
Looping
CommandlineArguments
Comparisons
VariableManipulations
KshRegular Expressions
Functions
DataRedirection
Pipes
Coprocesses
ReadInput from User and from Files
SpecialVariables
Actionon Success or Failure of a Command
TrivialCalculations
NumericalCalculations using "bc"


Principle of Script

Defining the Shell Type

To make a ksh script (which is a ksh program) crate a new file witha starting line like:
#!/usr/bin/ksh
It is important that the path to the ksh is propper and that theline doesn not have more than 32 characters. The shell from which youarestarting the script will find this line and and hand the whole scriptoverto to ksh. Without this line the script would be interpreted by the sametyp of shell as the one, from which it was started. But since the syntaxis different for all shells, it is necessary to define the shell withthatline.

Four Types of Lines

A script has four types of lines: The shell defining line at the top,empty lines, commentary lines starting with a #and command lines. See the following top of a script as an example forthese types of lines:

#!/usr/bin/ksh

# Commentary......

file=/path/file
if [[ $file = $1 ]];then
command
fi

Start and End of Script

The script starts at the first line and ends either when itencountersan "exit" or the last line. All"#" lines are ignored.

Start and End of Command

A command starts with the first word on a line or if it's the secondcommand on a line with the first word after a";'.
A command ends either at the end of the line or whith a ";".So one can put several commands onto one line:

print -n "Name: "; read name; print ""


One can continue commands over more than one line with a "\"immediately followed by a newline sign which is made be the return key:

grep filename | sort -u | awk '{print $4}' | \
uniq -c >> /longpath/file

Name and Permissions of Script File

The script mus not have a name which is identical to a unix command:So the script must NOT be called "test"!
After saveing the file give it the execute permissions with: chmod700 filename.


Variables

Filling in

When filling into a variable then one uses just it's name: state="US"and no blanks. There is no difference between strings and numbers: price=50.

Using

When using a variable one needs to put a $sign in front of it: print $state $price.

Arrays

Set and use an array like:

arrname[1]=4 To fill in
print ${arraname[1]} To print out
${arrname[*]} Get all elements
${#arrname[*]} Get the number of elements

#!/usr/bin/ksh

array[1]="hello"
array[2]="world"
array[3]="camel"
print${array[*]}
print ${array[1]}
print${#array[*]}


# ./array.sh
helloworld camel
hello
3

Declaration

There are happily no declarations of variables needed in ksh. Onecannothave decimals only integers.


Branching

if then fi

if [[ $value -eq 7 ]];then
print "$value is 7"
fi
or:

if [[ $value -eq 7 ]]
then
print "$value is 7"
fi
or:

if [[ $value -eq 7 ]];then print "$value is 7";fi

if then else fi

if [[ $name = "John" ]];then
print "Your welcome, ${name}."
else
print "Good bye, ${name}!"
fi

if then elif then else fi

if [[ $name = "John" ]];then
print "Your welcome, ${name}."
elif [[ $name = "Hanna" ]];then
print "Hello, ${name}, who are you?"
else
print "Good bye, ${name}!"
fi

case esac

case $var in
john|fred) print $invitation;;
martin) print $declination;;
*) print "Wrong name...";;
esac


Looping

while do done

while [[ $count -gt 0 ]];do
print "\$count is $count"
(( count -= 1 ))
done

until do done

until [[ $answer = "yes" ]];do
print -n "Please enter \"yes\": "
read answer
print ""
done

for var in list do done

for foo in $(ls);do
if [[ -d $foo ]];then
print "$foo is a directory"
else
print "$foo is not a directory"
fi
done

continue...break

One can skip the rest of a loop and directly go to the next iterationwith: "continue".

while read line
do
if [[ $line = *.gz ]];then
continue
else
print $line
fi
done

One can also prematurely leave a loop with: "break".

while read line;do
if [[ $line = *!(.c) ]];then
break
else
print $line
fi
done


Command LineArguments

(Officially they are called "positional parameters")

The number of command line arguments is stored in $#so one can check
for arguments with:

if [[ $# -eq 0 ]];then
print "No Arguments"
exit
fi

The single Arguments are stored in $1, ....$nand all are in $* as one string. Theargumentscannot
directly be modified but one can reset the hole commandline for anotherpart of the program.
If we need a first argument $first for the rest of the program we do:

if [[ $1 != $first ]];then
set $first $*
fi

One can iterate over the command line arguments with the help of theshift command. Shift indirectly removes the firstargument.

until [[ $# -qe 0 ]];do
# commands ....
shift
done

One can also iterate with the for loop, the default with for is $*:

for arg;do
print $arg
done

The program name is stored in $0 but itcontains the path also!


Comparisons

To compare strings one uses "="for equal and "!=" for not equal.
To compare numbers one uses "-eq"for equal "-ne" for not equal aswell as "-gt" for greater than
and "-lt" for less than.

if [[ $name = "John" ]];then
# commands....
fi
if [[ $size -eq 1000 ]];then
# commands....
fi

With "&&" for "AND" and "||" for"OR" one can combine statements:

if [[ $price -lt 1000 || $name = "Hanna" ]];then
# commands....
fi
if [[ $name = "Fred" && $city = "Denver" ]];then
# commands....
fi


VariableManipulations

Removing something from a variable

Variables that contain a path can very easily be stripped of it: ${name##*/}gives you just the filename.
Or if one wants the path: ${name%/*}. %takesit away from the left and # from the right.
%% and ## take the longest possibility while % and # just take theshortest one.

例如:

#!/usr/bin/ksh

print'${0#*/}='${0#*/}
print '${0##*/}='${0##*/}
print'${0%/*}='${0%/*}
print '${0%%/*}='${0%%/*}

#lxs/shellscript/strip.sh
${0#*/}=shellscript/strip.sh
${0##*/}=strip.sh
${0%/*}=lxs/shellscript
${0%%/*}=lxs

Replacing a variable if it does not yet exits

If we wanted $foo or if not set 4 then: ${foo:-4}but it still remains unset. To change that we use:
${foo:=4}

Exiting and stating something if variable is not set

This is very important if our program relays on a certain vaiable: ${foo:?"foonot set!"}

例如:此時foo沒有賦值;

#!/usr/bin/ksh

#foo="camel"
print${foo:?"foo not set"}
print "hello world"

# ./tt.sh
./tt.sh[4]:foo: foo not set

下面的例子將foo賦值,則hello world被輸出;

#!/usr/bin/ksh

foo="camel"
print${foo:?"foo not set"}
print "hello world"

# ./tt.sh
camel
helloworld

Just check for the variable

${foo:+1} gives one if $foo is set,otherwisenothing.


Ksh RegularExpressions

Ksh has it's own regular expressions.
Use an * for any string. So to get all thefiles ending it .c use *.c.
A single character is represented with a .So all the files starting with any sign followed bye 44.f can be fetchedby: 44.f.

Especially in ksh there are quantifiers for whole patterns:

(pattern) matches zero or one times thepattern.
*(pattern) matches any time the pattern.
+(pattern) matches one or more time thepattern.
@(pattern) matches one time the pattern.
!(pattern) matches string without thepattern.

So one can question a string in a variable like: if[[ $var = fo@(?4*67).c ]];then ...


Functions

Description

A function (= procedure) must be defined before it is called, becauseksh is interpreted at run time.
It knows all the variables from the calling shell except the commandlinearguments. But has it's
own command line arguments so that one can call it with different valuesfrom different places in
the script. It has an exit status but cannot return a value like a cfuncitioncan.

Making a Function

One can make one in either of the following two ways:

function foo {
# commands...
}

foo(){
# commands...
}

Calling the Function

To call it just put it's name in the script: foo. To give itargumentsdo: foo arg1 arg2 ...
The arguments are there in the form of $1...$nand $* for all at once like in the maincode.
And the main $1 is not influenced bye the$1 of a particular function.

Return

The return statement exits the functionimediately with the specified return value as an exit status.

例如:

#!/usr/bin/ksh

print$1
function foo {
print $1
return 100
}

foo "helloworld"
if [[ $? -eq 100 ]]; then
        print "foo is successful"
fi
#./fnc.sh camel
camel
hello world
foo is successful


Data Redirection

General

Data redirection is done with the follwoing signs: ">>> < <<". Every program has at least a

standardinput, standardoutput and standarderroroutput. All of thesecan be redirected.

Command Output to File

For writing into a new file or for overwriting a file do: command> file

For appending to a file do: command >>file

Standard Error Redirection

To redirect the error output of a command do: command2>file

To discard the error alltogether do: command2>/dev/null

To put the error to the same location as the normal output do: command2>&1

File into Command

If a program needs a file for input over standard input do: command< file

Combine Input and Output Redirection

command < infile > outfile
command < infile > outfile 2>/dev/null

Commands into Program ( Here Document )

Every unix command can take it's commands from a text like listingwith:

command <<EOF
input1
input2
input3
EOF

From eof to eof all is feeded into the above mentioned command.


Pipes

Fora serial processing of data from one command to the next do:
command1 | command2 | command3 ...
e.g. last | awk '{print $1}' | sort -u.


Coprocesses

One can have one background process with which one can comunicatewithread -p and print -p.It is started with command |&. If oneuses: ksh |& then this shell in thebackgroundwill do everything for us even telnet and so on: print-p"telnet hostname".


Read Input fromUserand from Files

Read in a Variable

From a user we read with: read var. Thenthe users can type something in. One should first print something like:print -n "Enter your favorite haircolor: ";readvar; print "". The -n suppresses the newline sign.

Read into a File Line for Line

To get each line of a file into a variable iteratively do:

{ while read myline;do
# process $myline
done } < filename

To catch the output of a pipeline each line at a time in a variableuse:

last | sort | {
while read myline;do
# commands
done }


Special Variables

$# Numberof argumentson commandline.
$? Exit status oflastcommand.
$$ Process id ofcurrentprogram.
$! Process id oflastbackgroundjob or background function.
$0 Program nameincludingthe path if started from another directory.
$1..n Commandlinearguments,each at a time.
$* All commandlineargumentsin one string.



Action on Successor on Failure of a Command

If one wants to do a thing only if a command succeded then: command1&& command2. If the second command has to be performedonlyif the first one failed, then: command1 ||command2.


TrivialCalculations

Simpe calculations are done with either a "let" in front ofit or within (( ... )). One can increment a variable within the (( ))withouta "$": (( a+=1 )) or leta+=1.

#!/usr/bin/ksh

n=0
sum=0
while [[ $n -le 100 ]] {
       ((sum += n))
        ((n += 1))
}
print "\$sum="$sum
[root@LuoXinshengshellscript]# ./let.sh
$sum=5050



NumericalCalculationsusing "bc"

For bigger caluculations one uses "bc" like: $result=$(print"n=1;for(i=1;i<8;i++)n=i*n;n"|bc)


"grep"

Search for the occurence of a pattern in a file: grep'pattern' file. If one just wants to know how often soemthingoccursin a file, then: grep -c 'pattern file.Thiscan be used in a script like:
if [[ $(grep -c 'pattern' file) != 0 ]];then......;fi.The condition is fullfilled if the pattern was found.


"sed"

Sed means stream line editor. It searches like grep, but is then ableto replace the found pattern. If you want to change all occurences of"poor"with "rich", do: sed -e 's/poor/rich/g'filename. Or what is often seen in software packages, that haveto be compiled after getting a propper configuration, is a whole filestuffedwith replacements patterns like: /@foo@/s;;king;g.This file with inumerable lines like that has to be given to sed with:sed -f sedscript filename. It thenprecesseseach line from file with all the sed commands in the sedscript. (Ofcoursesed can do much more:-))


"awk"

Awk can find and process a found line with several tools: It canbranch,loop, read from files and also print out to files or to the screen, andit can do arithmetics.
For example: We have a file with lines like: Fred 300 45 70 buthundreds of them. But some lines have a "#" as the first signof them and we have to omit these ones for both, processing and output.And we want to have lines as output like: 415 Fred where 415 isthe sum of 300, 45 and 70. Then we call on awk:

awk '$1 !~ /^#/ && $0 ~ /[^ ]/{print$2+$3+$4,"\t",$1}' filename.

This ignores lines with a "#" at the beginning of the firstfield and also blank lines. It then prints the desired sum and the $1istonly printed after a tab. This is the most trivial use of awk only.

Check my AWKprogramming introductionbye clicking on this sentence!


"perl"

Perl is a much richer programming language then ksh, but still onecando perl commands from within a ksh script. This might touch Randal, butit's true. Let's say you want to remove all ^M from a file, then takeperlfor one line in your ksh script:

perl -i -ep 's/\015//g' filename.

Perl can do an infinite amount of things in many different ways. Foranything bigger use perl instead of a shell script.

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
linux shell中的單引號與雙引號的區(qū)別(看完就不會有引號的疑問了)
Bash by example, Part 1
Intermediate UNIX Tutorial Notes
What’s Wrong With GNU make?
How I boosted my Vim ? nvie.com
Python文件流
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服