Linux Shell Script 教学

更新时间:2023-06-16 16:11:07 阅读: 评论:0

当归贝母苦参丸How to write a shell script
Introduction
A shell is a command line interpretor. It takes commands and executes them. As such, it implements a programming language. The Bourne shell is ud to create shell scripts -- ie. programs that are interpreted/executed by the shell. You can write shell scripts with the C-shell; however, this is not covered here.
Creating a Script
Suppo you often type the command
find . -name file -print
and you'd rather type a simple command, say
sfind file
Create a shell script
% cd ~/bin
% emacs sfind
% page sfind酒的广告
find . -name $1 -print
% chmod a+x sfind
% rehash
% cd /usr/local/bin
% sfind tcsh
./shells/tcsh
Obrvations
This quick example is far from adequate but some obrvations:
1. Shell scripts are simple text files created with an editor.
2. Shell scripts are marked as executeable
%chmod a+x sfind
3. Should be located in your arch path and ~/bin should be in your arch path.
4. You likely need to rehash if you're a Csh (tcsh) ur (but not again when you login).
5. Arguments are pasd from the command line and referenced. For example, as $1.
#!/bin/sh
All Bourne Shell scripts should begin with the quence
#!/bin/sh
From the man page for exec(2):空城计的故事简介
"On the first line of an interpreter script, following the "#!", is the name of a program which should be
ud to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the con- tents of the file are executed as a shell script."
You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but the days there are many interpretors -- Csh, Ksh, Bash, and others.
Comments
Comments are any text beginning with the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line.
Search Path
All shell scripts should include a arch path specifica- tion:
PATH=/usr/ucb:/usr/bin:/bin; export PATH
A PATH specification is recommended -- often times a script will fail for some people becau they have a different or incomplete arch path.
The Bourne Shell does not export environment variables to children unless explicitly instructed to do so by using the export command.
Argument Checking
A good shell script should verify that the arguments sup- plied (if any) are correct.
if [ $# -ne 3 ]; then
echo 1>&2 Usage: $0 19 Oct 91
exit 127
fi
This script requires three arguments and gripes accordingly.
Exit status
All Unix utilities should return an exit status.
# is the year out of range for me?
if [ $year -lt 1901  -o  $year -gt 2099 ]; then
echo 1>&2 Year \"$year\" out of range
蒜香牛蛙
exit 127
fi
<
# All done, exit ok
exit 0
A non-zero exit status indicates an error condition of some sort while a zero exit status indicates things worked as expected.
On BSD systems there's been an attempt to categorize some of the more common exit status codes. See
/usr/include/syxits.h.
Using exit status烤鱼的制作方法
Exit codes are important for tho who u your code. Many constructs test on the exit status of a command. The conditional construct is:
if command; then
command
fi
For example,
if tty -s; then
老虎宝典echo Enter text end with \^D
fi
Your code should be written with the expectation that others will u it. Making sure you return a meaningful exit status will help.
Stdin, Stdout, Stderr
Standard input, output, and error are file descriptors 0, 1, and 2. Each has a particular role and should be ud accordingly:
# is the year out of range for me?
if [ $year -lt 1901  -o  $year -gt 2099 ]; then
风景无限好
echo 1>&2 Year \"$year\" out of my range
exit 127
fi
<
# ok, you have the number of days since Jan 1, ...俄式西餐
ca `expr $days % 7` in
0)
echo Mon;;
1)
echo Tue;;
<
Error messages should appear on stderr not on stdout! Output should appear on stdout. As for input/output dialogue:
# give the fellow a chance to quit

本文发布于:2023-06-16 16:11:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1041274.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:简介   苦参   烤鱼
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图