Shell脚本攻略读书笔记之六

Shell脚本攻略读书笔记之六

查看时区 date -R
更改时区 tzselect
查看时间 date
查看日历 cal
修改时间 date -s

  • 11/28/2016【接日期】 将时间修改为2016年11月28日0点
  • 0:16:11【接时间】 修改当前时间为 0:16:11
[root@server4 local]# date -s 11/11/2020
Wed Nov 11 00:00:00 CST 2020
[root@server4 local]# date -s 0:16:12
Wed Nov 11 00:16:12 CST 2020
centos 7修改时区【这个可能与在系统初始化时有关】
[root@server4 local]# date
Sat Jul  7 04:51:47 EDT 2018
[root@server4 local]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@server4 local]# date
Sat Jul  7 16:54:18 CST 2018



[root@server3 cm]# 
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)

1.软连接的问题
3.重要的参数
netstat -a | grep 6000


associative arrays:关联数组。关联数组借用的是散列(hash)函数。
Initially,a declaration statement is required to declare a variable name as an associative array. This can be done as follows:
declare -A ass_array

[root@server4 ~]# alias hello = 'echo hello'
-bash: alias: hello: not found
-bash: alias: =: not found
-bash: alias: echo hello: not found
[root@server4 ~]# alias hello='echo hello'
[root@server4 ~]# hello
hello
[root@server4 ~]# hello
hello
[root@server4 ~]# hi
bash: hi: command not found...


为什么我定义成如下的样子,却跑不出来:alias rm='cp $@ ~/backup && rm $@'
See how to indentify them.



The attacker might have aliased the privileged command with his/her own custom command to steal the critical information that is provided by the user to the command.



alias命令

The -echo option in the preceding command disables the output to the terminal, whereas echo enables output.
选项-echo禁止将输出发送到终端,而选项echo则允许发送输出。



[root@server4 ~]# ./test4.sh
Enter password:

The password your input is 123456
[root@server4 ~]# 
echo 
-e参数:enable interpretation of backslash escapes【支持使用转义字符】
-n参数:do not output the trailing newline【不输出换行】

backslash:反斜杠

date: %s     seconds since 1970-01-01 00:00:00 UTC

用tput sc存储光标的位置。
在每次循环中,我们通过恢复之前存储的光标位置,在终端中打印出新的count值。
tput rc:恢复光标位置
tput ed清除从当前光标位置到行尾之间的所有内容。


set -x表示调试开始
set +x表示调试结束
aforementioned:上述的


Shebang hack
The shebang can be changed from #!/bin/bash to #!/bin/bash -xv to enable
debugging without any additional flags (-xv flags themselves).
这句话的意思是,如果脚本开头是#!/bin/bash,那么当我们想要调试时,我们需要使用-xv这个参数。但是当脚本开头是#!/bin/bash -xv开头时,就可以直接开启debug模式了。








[root@server4 ~]# mkdir backup
[root@server4 ~]# ll
total 1
drwxr-xr-x. 2 root root        6 Jul 25 18:55 backup

做删除
[root@server4 ~]# alias rm='cp $@ ~/backup && rm $@'

[root@server4 ~]# rm -rf out.txt 
cp: missing destination file operand after ‘/root/backup’
Try 'cp --help' for more information.
[root@server4 ~]# cd backup/
[root@server4 backup]# pwd
/root/backup

[root@server4 backup]# rm  out.txt 
cp: missing destination file operand after ‘/root/backup’
Try 'cp --help' for more information.

[root@server4 ~]# alias copy='cp $@ ~/backup'
[root@server4 ~]# ll
total 140284
-rw-r--r--. 1 root root       92 Jul 13 16:46 1
-rwxr-xr-x. 1 root root      339 Jul 17 13:54 addScalaHome.sh
-rwxr-xr-x. 1 root root      377 Jul 11 20:58 adjustSysctlConf.sh
-rw-------. 1 root root     1260 Jun 16 01:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root        6 Jul 25 18:55 backup
-rw-r--r--. 1 root root        9 Jul 20 21:02 cal
-rwxr-xr-x. 1 root root       99 Jul 20 20:39 checkUser.sh
-rwxr-xr-x. 1 root root      115 Jul 13 17:00 checkZookeeper.sh
-rwxr-xr-x. 1 root root      155 Jul 22 17:20 closeZookeeper.sh
drwxr-xr-x. 4 root root       37 Jul 13 11:01 hadoop_temp
-rw-r--r--. 1 root root       54 Jul  4 14:11 HelloLinux.txt
-rw-r--r--. 1 root root       42 Jul 22 20:56 hosts
-rwxr-xr-x. 1 root root       93 Jul 13 17:16 jps.sh
-rw-r--r--. 1 root root       67 Jul 25 11:24 log.txt
-rw-r--r--. 1 root root 78066230 Jul 13 17:36 opentsdb-2.3.0.tar.gz
drwxr-xr-x. 3 root root     8192 Jul 22 16:21 openTSDB_temp
-rw-r--r--. 1 root root       15 Jul 25 14:22 output.txt
-rw-r--r--. 1 root root      216 Jul 23 22:00 out.txt
-rwxr-xr-x. 1 root root       71 Jul 22 16:56 reboot.sh
-rw-r--r--. 1 root root 28678231 Mar  4  2016 scala-2.11.8.tgz
-rwxr-xr-x. 1 root root      267 Jul 13 17:01 startZookeeper.sh
-rw-r--r--. 1 root root       58 Jul 23 09:43 stderr.txt
-rw-r--r--. 1 root root        0 Jul 23 09:43 stdout.txt
-rw-r--r--. 1 root root     3497 Jul 20 19:19 temp
-rwxr-xr-x. 1 root root      138 Jul 20 14:40 test2.sh
-rwxr-xr-x. 1 root root      102 Jul 25 11:22 test3.sh
-rwxr-xr-x. 1 root root       98 Jul 20 14:41 test.sh
-rw-r--r--. 1 root root     3454 Jul 22 21:05 test.zip
-rw-r--r--. 1 root root 36670230 Jul  7 15:36 zookeeper-3.4.11.tar.gz
-rw-r--r--. 1 root root   124241 Jul 22 20:55 zookeeper.out
[root@server4 ~]# copy stdout.txt 
cp: omitting directory ‘/root/backup’

[root@server4 ~]# hello
[root@server4 ~]# 
[root@server4 ~]# tput cols
[root@server4 ~]# 
[root@server4 ~]# tput lines
[root@server4 ~]# 
[root@server4 ~]# tput lines
[root@server4 ~]# 
[root@server4 ~]# tput cols
[root@server4 ~]# 
[root@server4 ~]# tput logname
[root@server4 ~]# info capability 'logname'
[root@server4 ~]# tput longname
[root@server4 ~]# lator (X Window System)[root@server4 ~]# 
[root@server4 ~]# 
[root@server4 ~]# tput cup 100 100
[root@server4 ~]#                                                                                   [
[root@server4 ~]# tput cup 10 10
[root@server4 ~]# 
[root@server4 ~]# tput setb n
[root@server4 ~]# tput setb 2
[root@server4 ~]# tput setb 0
[root@server4 ~]# hello
hello
[root@server4 ~]# hello
hello
[root@server4 ~]# owca 
bash: owca: command not found...
[root@server4 ~]# tput setb 1
[root@server4 ~]# echo this
this
[root@server4 ~]# echo this
this
[root@server4 ~]# haha 
bash: haha: command not found...
[root@server4 ~]# tput smul
[root@server4 ~]# tput rmul
[root@server4 ~]# ll
total 140288
-rw-r--r--. 1 root root       92 Jul 13 16:46 1
-rwxr-xr-x. 1 root root      339 Jul 17 13:54 addScalaHome.sh
-rwxr-xr-x. 1 root root      377 Jul 11 20:58 adjustSysctlConf.sh
-rw-------. 1 root root     1260 Jun 16 01:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root        6 Jul 25 18:55 backup
-rw-r--r--. 1 root root        9 Jul 20 21:02 cal
-rwxr-xr-x. 1 root root       99 Jul 20 20:39 checkUser.sh
-rwxr-xr-x. 1 root root      115 Jul 13 17:00 checkZookeeper.sh
-rwxr-xr-x. 1 root root      155 Jul 22 17:20 closeZookeeper.sh
drwxr-xr-x. 4 root root       37 Jul 13 11:01 hadoop_temp
-rw-r--r--. 1 root root       54 Jul  4 14:11 HelloLinux.txt
-rw-r--r--. 1 root root       42 Jul 22 20:56 hosts
-rw-r--r--. 1 root root     1308 Jul 25 21:13 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root       93 Jul 13 17:16 jps.sh
-rw-r--r--. 1 root root       67 Jul 25 11:24 log.txt
-rw-r--r--. 1 root root 78066230 Jul 13 17:36 opentsdb-2.3.0.tar.gz
drwxr-xr-x. 3 root root     8192 Jul 22 16:21 openTSDB_temp
-rw-r--r--. 1 root root       15 Jul 25 14:22 output.txt
-rw-r--r--. 1 root root      216 Jul 23 22:00 out.txt
-rwxr-xr-x. 1 root root       71 Jul 22 16:56 reboot.sh
-rw-r--r--. 1 root root 28678231 Mar  4  2016 scala-2.11.8.tgz
-rwxr-xr-x. 1 root root      267 Jul 13 17:01 startZookeeper.sh
-rw-r--r--. 1 root root       58 Jul 23 09:43 stderr.txt
-rw-r--r--. 1 root root        0 Jul 23 09:43 stdout.txt
-rw-r--r--. 1 root root     3497 Jul 20 19:19 temp
-rwxr-xr-x. 1 root root      138 Jul 20 14:40 test2.sh
-rwxr-xr-x. 1 root root      102 Jul 25 11:22 test3.sh
-rwxr-xr-x. 1 root root       98 Jul 20 14:41 test.sh
-rw-r--r--. 1 root root     3454 Jul 22 21:05 test.zip
-rw-r--r--. 1 root root 36670230 Jul  7 15:36 zookeeper-3.4.11.tar.gz
-rw-r--r--. 1 root root   124241 Jul 22 20:55 zookeeper.out





[root@server4 ~]# echo \\
\

输出当前日期
[root@server4 ~]# date +%s
1532525664



[root@server4 ~]# cat out.txt
HelloLinux
/root
this is third line.added in 20180704
HelloLinux
/root
this is third line.added in 20180704
HelloLinux
/root
this is third line.added in 20180704
HelloLinux
/root
this is third line.added in 20180704
Thu Jul 26 10:22:01 CST 2018
Thu Jul 26 10:23:01 CST 2018
[root@server4 ~]# 


[root@server4 cron]# cat root 
* * * * * /bin/echo `date` >> /root/out.txt
[root@server4 cron]# 



`echo > file`【echo 12 >> file2】命令:
1.即使当前目录下没有file文件,也会自己创建一个file文件
[littlelawson@server4 test]$ ll
total 0
[littlelawson@server4 test]$ echo 12 > file1
[littlelawson@server4 test]$ ll
total 4
-rw-rw-r--. 1 littlelawson littlelawson 3 Jul 27 10:14 file1

unalias vi
问题:vi 打开文件时,蓝色字体和黑色背景导致阅读困难
原因:有时候并不是因为系统的原因,而是因为xshell的字体风格设置的原因。
解决办法:调整xshell的配色方案。

dd连敲两行


###shell脚本攻略学习笔记之六
concatenate content from input files along with standard input using cat. Combine stdin and data from another file,as follows:
echo 'Text through stdin' | cat - file.txt

cat 
-s --squeeze-blank
    suppress repeated empty output lines/squeeze adjacent blank lines.[压缩连续的空行]
-T, --show-tabs
    display TAB characters as ^I[将TAB键展示城^I]

[root@server4 ~]# cat file.py
def function()
    var = 5;
           next = 6
    third = 7
[root@server4 ~]# cat -T file.py
def function()
^Ivar = 5;
           next = 6
^Ithird = 7

-n, --number
    number all output lines【显示出行号,包括空行】

-b, --number-nonblank
    number nonempty output lines, overrides -n【输出行号,不包括空行】


Recording and playing back of terminal sessions
这里的意思就是:如果需要查看过去的终端回话的的历史,那么我们可以将我们执行的命令存放到某个文件件中,以供后期查看。

[root@server4 ~]# script
Script started, file is typescript
[root@server4 ~]# echo hello
hello
[root@server4 ~]# exit
exit
Script done, file is typescript
[root@server4 ~]# ll
total 1
-rw-r--r--. 1 root root      241 Aug  5 11:59 typescript
[root@server4 ~]# cat typescript
Script started on Sun 05 Aug 2018 11:54:28 AM CST
[root@server4 ~]# echo hello
hello
[root@server4 ~]# exit
exit

Script done on Sun 05 Aug 2018 11:59:17 AM CST

script 
 -a, --append
    Append the output to file or typescript, retaining the prior contents.【追加输出到file或者是typescript中,保留之前的内容】

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/81485332