Shell脚本攻略读书笔记之五
shell 脚本 攻略 读书笔记 之五
2023-09-14 09:13:15 时间
Shell脚本攻略读书笔记之五
fname()
{
echo $1,$2;
echo "$@";
echo "$*";
return 0;
}
$@比$*用的多,因为$@将所有的参数作为一个单个字符串。$*将每个参数作为单个实体。
递归函数
F()
{
echo $1;
F hello;
sleep 1;
}
当我们结合多个命令时,我们通常使用stdin到给定输入,或者stdout到给定输出。在这些的上下文中,命令被称之为filters。
While we combine multiple commands, we usually use stdin to give input and stdout to provide an output.In this context, the commands are called filters. We connect each filter using pipes, the piping operator being |. An example is as follows:
$ cmd1 | cmd2 | cmd3
Here we combine three commands. The output of cmd1 goes to cmd2 and output of cmd2 goes to cmd3 and the final output (which comes out of cmd3) will be printed, or it can be directed to a file
cat
-n number all output lines【所有输出行】
[root@server4 ~]# pwd
/root
[root@server4 ~]# (cd /bin; ls);
[ gvfs-open python2
a2p gvfs-rename python2.7
abrt-action-analyze-backtrace gvfs-rm qemu-ga
abrt-action-analyze-c gvfs-save qemu-img
····
gvfs-monitor-file pygtk-demo znew
gvfs-mount pyinotify zsoelim
gvfs-move python
[root@server4 ~]# pwd
/root
pwd;
(cd /bin;ls)
pwd;
命令在子shell中的执行,不影响当前shell。可以使用一个()操作符去定义子shell。
Most of the input libraries in any programming language read the input from the keyboard; but string input termination is done when return is pressed.
[root@server4 ~]# read -p "Enter input:" -s var
Enter input:[root@server4 ~]# echo $var
fsdodf
[root@server4 ~]#
还是不能理解这个结束循环的功能:
repeat() {while true;do $@ && return; done}
repeat() {while :; do $@ && return; done }
repeat() {while :; do $@ && return; sleep 30;done}
internal field separator(IFS)
crontab重定向命令到终端。
date > /dev/tty1 #date输出重定向到需要的终端
-e支持转义
[root@server4 ~]# echo -e \a
a
data="name,sex,rollno,location"
oldIFS=$IFS #backUp
IFS=, #now, the internal field separator(IFS) is a comma
for item in $data;do; echo Item:$item;done
IFS=$oldIFS #reset
[root@server4 ~]# for item in $data; do echo Item:$item;done
Item:name
Item:sex
Item:rollno
Item:location
这个for循环,会按照分隔符自动将data分割。
[root@server4 ~]#
The default value of IFS is a space component (newline, tab, or a space character).
[ condition ] && action; # action executes
if the condition is true
[ condition ] || action; # action executes
if the condition is false
&& is the logical AND operation and || is the logical OR
operation. This is a very helpful trick while writing Bash scripts
[ $var1 -ne 0 -a $var2 -gt 2 ] # using and -a
变量var1不等于0,并且变量var2大于2
[ $var1 -ne 0 -o var2 -gt 2 ] # OR -o
变量var1不等于0,或者var2大于2
Note that a space is provided after and before =, if it is not provided, it is not a comparison, but it becomes an assignment statement.
相关文章
- 40个很有用的Mac OS X Shell脚本和终端命令
- VIM打开shell脚本中文乱码解决
- linux shell 脚本攻略学习16--wc命令详解,tree命令详解
- linux shell 脚本攻略学习15--如何只列出目录,如何快速切换目录
- linux shell 脚本攻略学习14--head命令详解,tail命令详解
- linux shell 脚本攻略学习13--file命令详解,diff命令详解
- Linux Shell脚本自动化编程实战- shell函数
- 获取shell脚本自身所在目录的Shell脚本分享
- Shell: 测试网卡当前速率的脚本
- linux shell 脚本攻略学习14--head命令详解,tail命令详解
- shell脚本进行数据库操作
- 批量建用户的shell小脚本
- Linux Shell脚本自动化编程实战- shell并发
- 获取shell脚本自身所在目录的Shell脚本分享
- Linux系统shell脚本之while循环实践1
- 【Linux之Shell脚本实战】猜拳小游戏shell脚本
- Liunx下的日志清理shell脚本实战之日志备份
- shell 实例脚本
- Shell 脚本实战之报警监控
- Linux shell 脚本攻略学习笔记八
- Bash/Shell-脚本整理
- S20.shell脚本每日一练