- 论坛徽章:
- 0
|
背景:为前前公司tcsh培训做的提纲,基本涵盖tcsh shell的知识。
Use TC shell
1.1 TC shell environment
1.1.1 ~/.tcshrc or ~/.cshrc
In this file you can initialize your environment, such as
alias
$prompt
$path
$cdpath
1.1.2 source command
source ~/.cshrc after you modified .cshrc or .tcshrc.
1.1.3 Command line
Shell split the string you enter to single word, the first word is command and other words are parameters.
1.1.4 Exit status variable
$status and $?
1.1.5 Command group
( command1 ; command2 ; command3 ) > filename
1.1.6 && and ||
command1 && command2
1.1.7 Background job
command &
1.1.8 History command
history | more
up and down arrow
!! !n !a
1.1.9 The completion of command, path, filename
Press TAB button
1.1.10 Alias
alias list alias list
alias c clear alias c to clear
unalias c cancel alias
1.2 Jobs control
jobs display running jobs
ctrl+z suspend current job
bg move job to background
fg move job to frontground
1.3 Some special sign
$ mark variable ! history command * none, one or more character
? one character ; compart command & background
> redirect >! Redirect and cover >> incremental redirect
>>! Incremental and create >& redirect stdin and stderr
‘’ and “”
1.4 Variable
1.4.1 Switch variable
Use set command to open or close it
1.4.2 Assignment variable
Use set var = value to create it or reassign it, and ues unset var to release it.
1.4.3 Inside variable
Such as environment variable.
1.4.4 Print variable
echo $var
printf formatted ouput
1.5 Array
1.5.1 Those words that splitted by space or table character and put in parentheses.
set names = ( Johnny Tom Mary )
$names and $names Johnny Tom Mary
$names[1] Johnny
$#names 3
1.5.2 Special variable
$?var return 1 if $var was assigned
$$ current shell Process ID
$ read var
$? Exit status
1.6 Command substitution
The output of those commands which between ` and ` can assign to a variable or be an input of another command.
rm –f `find ./ -user zhliao`
Program with TC shell
2.1 Three steps to create a script
● Create a text file using any one of text editor
● Edit the text file
First line: #!/bin/tcsh –f
Use # to mark comment
● Use chmod command to make the script executable
chmod u+x scriptname.tcsh
Run the script in command line (absolute path or opposite path)
/home/yourname/bin/scriptname.tcsh
or ./scriptname.tcsh
2.2 Read input
Variable $
Single word
set var = $
String
set var = “$
Create word list
set var = “$
set wordlist = ( $var )
echo $wordlist[1] $wordlist[2] …
2.3 Calculation (only integer)
Use @ to evaluate
+ - * / % …
+= -= *= /= %= ++ --
@ num = 4 + 6
@ num = $var1 + $var2
@ num += 2
@ num ++
For floating-point calculation bc or awk etc.
2.4 Command line parameter
$0 script name
$1,$2,…,${10},…,$n first,second,...,command line parameter
$* all parameters
Array argv for storing command line parameter
$argv[0] none
$argv[1],$argv[2] first,second,…,command line parameter
$argv or $argv all parameters
$#argv number of parameters
$argv[$#argv] the last parameter
scriptname.tcsh a b c d
2.5 Flow control and if-clause
2.5.1 Test expression and return true or false
Operator function operator function
== equal != not equal
> greater than >= greater than or equal to
less than less than or equal to
=~ string matching !~ string not matching
&& and
|| or
! not
● if sentence ● if/else sentence ● if sentence with single line
if ( expression ) then if ( expression ) then if ( expression ) command
command command1
command else
endif command2
endif
● if/else if sentence
if ( expression ) then
command1
else if ( expression )
then
command2
else
command3
endif
2.5.2 Exit status and status variable
$status = 0 if command execute favoring
$status = other value if command didn’t execute favoring
Variable $status can be influenced by this command
exit n n equal to an integer
You can use exit command to exit script.
2.5.3 if sentence and exit status
if { ( command1 ) } then
command2
endif
2.5.4 Test file’s property
Parameter function parameter function
-d directory -e exist
-o file’s owner -r read able
-w write able -x executable
-s not empty -z empty file
if ( -e /path/filename ) then
Command
endif
if ( -rwx /path/filename ) then if ( -r && -w && -x /path/fineneme ) then
command command
endif endif
2.5.5 Inside file’s property command
filetest –rwx file1 file2 …
2.5.6 switch sentence
switch ( var )
case constant1 :
commands
breaksw
case constant2 :
commands
breaksw
default :
commands
endsw
2.5.7 HERE document, be used with switch sentence
Here document is usually used to create menu for choosing in script.
echo “select a item :”
cat
1) …
2) …
3) …
EOF
set choice = $
switch ( $choice )
case …
endsw
2.6 Repetition
2.6.1 foreach
foreach var ( wordlist )
commands
end
Assign one word in wordlist to variable var each time and then enter the repetition, the assign next word, …
2.6.2 while
while ( expression )
commands
end
2.6.3 Some commands in repetition
repeat n command
shift delete the first parameter in left side of array argv
break n exit n repetitions
2.7 Store scripts and make them as Linux command
Mkdir ~/bin
Set path = ( $path ~/bin )
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/73914/showart_1099001.html |
|