高级 Bash 脚本编程指南
本教程内容转载自:https://github.com/LinuxStory/Advanced-Bash-Scripting-Guide-in-Chinese
感谢开源翻译!!!
本教程内容转载自:https://github.com/LinuxStory/Advanced-Bash-Scripting-Guide-in-Chinese
感谢开源翻译!!!
掌握 Linux 命令是编写有效的 shell 脚本必不可少的序曲。
本章节涉及了下列的命令:
.(参照 source)acadduseragettyagrepararchatautoloadawk(参考 使用 awk 进行数学运算)badblocksbannerbasenamebatchbcbgbindbisonbuiltinbzgrepbzip2calcallercatcdchattrchfnchgrpchkconfigchmodchownchrootcksumclearclockcmpcolcolrmcolumncommcommandcompgencompletecompresscoproccpcpiocroncryptcsplitcucutdatedcdddebugfsdeclaredepmoddfdialogdiffdiff3diffstatdigdirnamedirsdisowndmesgdoexecdos2unixdudumpdumpe2fse2fsckechoegrepenableenscriptenveqnevalexecexit(相关话题:退出状态)expandexportexprfactorfalsefdformatfdiskfgfgrepfilefindfingerflexflockfmtfoldfreefsckftpfusergetfaclgetoptgetoptsgettextgettygnome-mountgrepgroffgroupmodgroups(相关话题:变量 $GROUPS)gsgziphalthashhdparmheadhelphexdumphosthostidhostname(相关话题:变量 $HOSTNAME)hwclockiconvid(相关话题:变量 $UID)ifconfiginfoinfocmpinitinsmodinstallipipcalciptablesiwconfigjobsjoinjotkillkillalllastlastcommlastloglddlessletlexlidlnlocatelockfileloggerlognamelogoutlogrotatelooklosetuplplslsdevlsmodlsoflspcilsusbltracelynxlzcatlzmam4mailmailstatsmailtomakeMAKEDEVmanmapfilemcookiemd5summergemesgmimencodemkbootdiskmkdirmkdosfsmke2fsmkfifomkisofsmknodmkswapmktempmmencodemodinfomodprobemoremountmsgfmtmvncnetconfignetstatnewgrpnicenlnmnmapnohupnslookupobjdumpodopensslpasswdpastepatch(相关话题:diff)pathchkpaxpgreppidofpingpkillpopdprprintenvprintfprocinfopspstreeptxpushdpwd(相关话题:变量 $PWD)quotarcprdevrdistreadreadelfreadlinkreadonlyrebootrecodereniceresetresizerestorerevrloginrmrmdirrmmodrouterpmrpm2cpiorshrsyncrunlevelrun-partsrxrzsarscpscriptsdiffsedseqservicesetsetfaclsetquotasetserialsettermsha1sumsharshoptshredshutdownsizeskillsleepslocatesnicesortsourcesoxsplitsqsshstatstracestringsstripsttysusudosumsuspendswapoffswaponsxsyncsztactailtartbltcpdumpteetelinittelnetTextexexectimetimestmpwatchtoptouchtputtrtraceroutetruetsettsortttytune2fstypetypesetulimitumaskumountunameunarcunarjuncompressunexpanduniqunitsunlzmaunrarunsetunsqunzipuptimeusbmodulesuseradduserdelusermodusersusleepuucpuudecodeuuencodeuuxvacationvdirvmstatvrfywwaitwallwatchwcwgetwhatiswhereiswhichwhowhoamiwhoiswritexargsxrandrxzyaccyeszcatzdiffzdumpzegrepzfgrepzgrepzipBash是唯一被允许执行的shell脚本语言。
可执行文件必须以#!/bin/bash和最小数量的标志开始。请使set来设置shell的选项,使得用bash <script_name>调用你的脚本时不会破坏其功能。
限制所有的可执行shell脚本为bash使得我们安装在所有计算机中的shell语言保持一致性。
crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。
crontab(选项)(参数)
eth0 以太网接口
lo 本地环回接口
br0 桥接接口 (虚拟交换机)
vnet0 KVM虚拟机网卡接口
ip addr //查看IP、掩码、MAC...
ip addr show eth0 //只显示eth0的信息
ip route //查看本机路由表
cat /etc/resolv.conf //查看DNS
/etc/security/limits.conf 文件实际是 Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,而且只针对于单个会话。 该设置不会影响系统服务的资源限制。还要注意 /etc/security/limits.d/ 的这个目录,
ps命令用于报告当前系统的进程状态。可以搭配kill指令随时中断、删除不必要的程序。ps命令是最基本同时也是非常强大的进程查看命令,使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等,总之大部分信息都是可以通过执行该命令得到的。
运行(正在运行或在运行队列中等待)
中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)
不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生)
僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放)
停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行)