shell keyword;buildin;and command

本文介绍了Bash中的内建命令及其与外部命令的区别。内建命令直接在Bash内部实现,无需fork新进程,因此执行效率更高。文章还提供了一个示例脚本来展示命令的fork行为,并列举了常见的Bash内建命令。

 内建命令指的就是包含在Bash工具集中的命令.这主要是考虑到执行效率的问题--内建命令将
比外部命令的执行得更快,外部命令通常需要fork出一个单独的进程来执行.另外一部分原因
是特定的内建命令需要直接存取shell内核部分.
当一个命令或者是shell本身需要初始化(或者创建)一个新的子进程来执行一个任务的时候,这
种行为被称为forking.这个新产生的进程被叫做子进程,并且这个进程是从父进程中分离出来
的.当子进程执行它的任务时,同时父进程也在运行.
注意:当父进程取得子进程的进程ID的时候,父进程可以传递给子进程参数,而反过来则不行.
这将产生不可思议的并且很难追踪的问题.
Example 11-1 一个fork出多个自己实例的脚本
################################Start Script#######################################
#!/bin/bash
# spawn.sh
PIDS=$(pidof sh $0) # Process IDs of the various instances of this script.
P_array=( $PIDS ) # Put them in an array (why?).
echo $PIDS # Show process IDs of parent and child processes.
let "instances = ${#P_array[*]} − 1" # Count elements, less 1.
# Why subtract 1?
echo "$instances instance(s) of this script running."
echo "[Hit Ctl−C to exit.]"; echo
sleep 1 # Wait.
sh $0 # Play it again, Sam.
exit 0 # Not necessary; script will never get to here.
# Why not?
# After exiting with a Ctl−C,
#+ do all the spawned instances of the script die?
# If so, why?
# Note:
# −−−−
# Be careful not to run this script too long.
# It will eventually eat up too many system resources.
# Is having a script spawn multiple instances of itself
#+ an advisable scripting technique.
# Why or why not?
#Generally, a Bash builtin does not fork a subprocess when it executes within a script. An external system
#+command or filter in a script usually will fork a subprocess.


################################End Script#########################################
一般的,脚本中的内建命令在执行时将不会fork出一个子进程.但是脚本中的外部或过滤命令
通常会fork一个子进程.
一个内建命令通常与一个系统命令同名,但是Bash在内部重新实现了这些命令.比如,Bash的
echo命令与/bin/echo就不尽相同,虽然它们的行为绝大多数情况下是一样的.
1 #!/bin/bash
2
3 echo "This line uses the /"echo/" builtin."
4 /bin/echo "This line uses the /bin/echo system command."
关键字的意思就是保留字.对于shell来说关键字有特殊的含义,并且用来构建shell的语法结构.
比如,"for","while","do"和"!"都是关键字.与内建命令相同的是,关键字也是Bash的骨干部分,
但是与内建命令不同的是,关键字自身并不是命令,而是一个比较大的命令结构的一部分.

 

我们可利用type -a <command>来查看该命令是哪种类型,如:type -a echo type -a read etc.

 

buildin 命令主要有以下这些:

IO:echo printf read

Filesystem:cd pwd pushd popd dirs

Variables:let eval set unset export declare typeset readonly getopts

Script Behavor:source exit exec shopt caller

Commands:true false type hash bind help

Job control:jobs disown fg bg wait suspend logout times kill command builtin enable autoload

                                                            from abs_guide_en

                                                                        

[root@iZgmsn2rycs3miZ ~]# yum --help usage: yum [options] COMMAND List of Main Commands: alias List or create command aliases autoremove remove all unneeded packages that were originally installed as dependencies check check for problems in the packagedb check-update check for available package upgrades clean remove cached data deplist [deprecated, use repoquery --deplist] List package's dependencies and what packages provide them distro-sync synchronize installed packages to the latest available versions downgrade Downgrade a package group display, or use, the groups information help display a helpful usage message history display, or use, the transaction history info display details about a package or group of packages install install a package or packages on your system list list a package or groups of packages makecache generate the metadata cache mark mark or unmark installed packages as installed by user. module Interact with Modules. provides find what package provides the given value reinstall reinstall a package remove remove a package or packages from your system repolist display the configured software repositories repoquery search for packages matching keyword repository-packages run commands on top of all packages in given repository search search package details for the given string shell run an interactive YUM shell swap run an interactive YUM mod for remove and install one spec updateinfo display advisories about packages upgrade upgrade a package or packages on your system upgrade-minimal upgrade, but only 'newest' package match which fixes a problem that affects your system List of Plugin Commands: builddep Install build dependencies for package or spec file changelog Show changelog data of packages config-manager manage yum configuration options and repositories copr Interact with Copr repositories. debug-dump dump information about installed rpm packages to file debug-restore restore packages recorded in debug-dump file debuginfo-install install debuginfo packages download Download package to current directory groups-manager create and edit groups metadata file needs-restarting determine updated binaries that need restarting offline-distrosync Prepare offline distrosync of the system offline-upgrade Prepare offline upgrade of the system playground Interact with Playground repository. repoclosure Display a list of unresolved dependencies for repositories repodiff List differences between two sets of repositories repograph Output a full package dependency graph in dot format repomanage Manage a directory of rpm packages reposync download all packages from remote repo system-upgrade Prepare system for upgrade to a new release General YUM options: -c [config file], --config [config file] config file location -q, --quiet quiet operation -v, --verbose verbose operation --version show YUM version and exit --installroot [path] set install root --nodocs do not install documentations --noplugins disable all plugins --enableplugin [plugin] enable plugins by name --disableplugin [plugin] disable plugins by name --releasever RELEASEVER override the value of $releasever in config and repo files --setopt SETOPTS set arbitrary config and repo options --skip-broken resolve depsolve problems by skipping packages -h, --help, --help-cmd show command help --allowerasing allow erasing of installed packages to resolve dependencies -b, --best try the best available package versions in transactions. --nobest do not limit the transaction to the best candidate -C, --cacheonly run entirely from system cache, don't update cache -R [minutes], --randomwait [minutes] maximum command wait time -d [debug level], --debuglevel [debug level] debugging output level --debugsolver dumps detailed solving results into files --showduplicates show duplicates, in repos, in list/search commands -e ERRORLEVEL, --errorlevel ERRORLEVEL error output level --obsoletes enables yum's obsoletes processing logic for upgrade or display capabilities that the package obsoletes for info, list and repoquery --rpmverbosity [debug level name] debugging output level for rpm -y, --assumeyes automatically answer yes for all questions --assumeno automatically answer no for all questions --enablerepo [repo] Enable additional repositories. List option. Supports globs, can be specified multiple times. --disablerepo [repo] Disable repositories. List option. Supports globs, can be specified multiple times. --repo [repo], --repoid [repo] enable just specific repositories by an id or a glob, can be specified multiple times --enable enable repos with config-manager command (automatically saves) --disable disable repos with config-manager command (automatically saves) -x [package], --exclude [package], --excludepkgs [package] exclude packages by name or glob --disableexcludes [repo], --disableexcludepkgs [repo] disable excludepkgs --repofrompath [repo,path] label and path to an additional repository to use (same path as in a baseurl), can be specified multiple times. --noautoremove disable removal of dependencies that are no longer used --nogpgcheck disable gpg signature checking (if RPM policy allows) --color COLOR control whether color is used --refresh set metadata as expired before running the command -4 resolve to IPv4 addresses only -6 resolve to IPv6 addresses only --destdir DESTDIR, --downloaddir DESTDIR set directory to copy packages to --downloadonly only download packages --comment COMMENT add a comment to transaction --bugfix Include bugfix relevant packages, in updates --enhancement Include enhancement relevant packages, in updates --newpackage Include newpackage relevant packages, in updates --security Include security relevant packages, in updates --advisory ADVISORY, --advisories ADVISORY Include packages needed to fix the given advisory, in updates --bz BUGZILLA, --bzs BUGZILLA Include packages needed to fix the given BZ, in updates --cve CVES, --cves CVES Include packages needed to fix the given CVE, in updates --sec-severity {Critical,Important,Moderate,Low}, --secseverity {Critical,Important,Moderate,Low} Include security relevant packages matching the severity, in updates --forcearch ARCH Force the use of an architecture [root@iZgmsn2rycs3miZ ~]#
10-14
yum命令的形式一般为:yum [options] [command] [package ...],其中[options]是可选的,[command]为所要进行的操作,[package ...]是操作的对象[^1]。 ### 选项说明 - `-h`:提供帮助信息。 - `-y`:在安装过程提示选择时全部回答"yes";在使用基本语法时,对所有提问都回答"Yes" [^1][^3]。 - `-q`:不显示安装的过程 [^1]。 ### 常用命令及功能 - `install`:安装RPM软件包 [^3]。 - `update`:更新RPM软件包 [^3]。 - `check-update`:检查是否有可用的更新RPM软件包 [^3]。 - `remove`:删除指定的RPM软件包 [^3]。 - `list`:显示软件包信息 [^3]。 - `clean`:清理yum过期的缓存 [^3]。 - `deplist`:显示yum软件包的所有依赖关系 [^3]。 - `yumdownloader <package-name> --resolve --enablerepo=[repo]`:下载rpm包,`--resolve`表示依赖包也会被下载,`--enablerepo=<repository>`可指定yum源,若不知可以省去此参数 [^5]。 ### 编辑配置文件 yum的一切信息都存储在`/etc/yum.repos.d`目录下的配置文件中。若要创建新的yum仓库配置文件,可在`/etc/yum.repos.d/local.repo`文件中添加本地仓库信息,包括仓库的ID、名称、基础URL(指向rpm包目录)以及是否启用GPG检查等 [^2][^4]。 ### 安装yum及其依赖 当需要从本地仓库安装yum包,若yum尚未安装或配置不正确,可使用rpm命令直接安装rpm包:`rpm -ivh /opt/yum-rpms/*.rpm --nodeps --force` [^2]。 ### 编辑相关配置文件 编辑`/etc/yum/pluginconf.d/fastestmirror.conf`文件示例如下: ```plaintext [main] enabled=1 verbose=0 always_print_best_host = true socket_timeout=3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值