随笔 - 55  文章 - 0  评论 - 0  阅读 - 1047

17_常用命令02

1. 查看文件后/前几行
# tail => 尾巴
# head => 头

# 查看后5行
[root@stream9 ~]# cat 1.txt
1
2
...
19
20
[root@stream9 ~]#
[root@stream9 ~]# tail -5 1.txt
16
17
18
19
20

# 默认看后10行
[root@stream9 ~]# tail 1.txt
12
13
14
15
16
17
18
19
20
21

# 前3行
[root@stream9 ~]# head -3 1.txt
1
2
3

# 试试查看谁登录该虚拟机的日志
[root@stream9 log]# pwd
/var/log    # 专门存放所有日志的目录
[root@stream9 log]# ls
anaconda  chrony           dnf.log      hawkey.log  lastlog   private  sa       sssd      wtmp
audit     cron             dnf.rpm.log  journal     maillog   qemu-ga  secure   tallylog
btmp      dnf.librepo.log  firewalld    kdump.log   messages  README   spooler  tuned
[root@stream9 log]# tail -f secure # 举例
Oct 16 16:05:04 stream9 su[4129]: pam_unix(su-l:session): session closed for user admin
Oct 16 17:52:53 stream9 sshd[4056]: pam_unix(sshd:session): session closed for user root
Oct 16 19:11:15 stream9 sshd[4319]: Failed password for root from 10.10.11.1 port 48430 ssh2
Oct 16 19:11:15 stream9 sshd[4319]: Failed password for root from 10.10.11.1 port 48430 ssh2
Oct 16 19:11:15 stream9 sshd[4319]: Connection closed by authenticating user root 10.10.11.1 port 48430 [preauth]
Oct 16 19:11:23 stream9 sshd[4321]: Accepted password for root from 10.10.11.1 port 33842 ssh2
Oct 16 19:11:23 stream9 systemd[4326]: pam_unix(systemd-user:session): session opened for user root(uid=0) by (uid=0)
Oct 16 19:11:23 stream9 sshd[4321]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Oct 16 20:59:11 stream9 sshd[4550]: Accepted password for root from 10.10.11.1 port 40728 ssh2
Oct 16 20:59:11 stream9 sshd[4550]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Oct 16 21:03:00 stream9 sshd[4335]: Received disconnect from 10.10.11.1 port 33842:11: disconnected by user
Oct 16 21:03:00 stream9 sshd[4335]: Disconnected from user root 10.10.11.1 port 33842
Oct 16 21:03:00 stream9 sshd[4321]: pam_unix(sshd:session): session closed for user root
Oct 16 21:03:18 stream9 sshd[4646]: Accepted password for root from 10.10.11.1 port 40300 ssh2
Oct 16 21:03:18 stream9 sshd[4646]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Oct 16 21:03:54 stream9 sshd[4648]: Received disconnect from 10.10.11.1 port 40300:11: disconnected by user
Oct 16 21:03:54 stream9 sshd[4648]: Disconnected from user root 10.10.11.1 port 40300
Oct 16 21:03:54 stream9 sshd[4646]: pam_unix(sshd:session): session closed for user root

2.crontab 定时执行任务
# 1. 查看帮助
[root@stream9 ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n <host>  set host in cluster to run users' crontabs
 -c         get host in cluster to run users crontabs
 -T <file>  test a crontab file syntax
 -s         selinux context
 -V         print version and exit
 -x <mask>  enable debugging

Default operation is replace, per 1003.2

# 2. 添加定时任务 每隔一分钟执行脚本writeTime.sh
chmod +x writeTime.sh # 给该脚本执行权限
[root@stream9 ~]# crontab -e -u root
[root@stream9 ~]# crontab -l -u root
*/1 * * * *  /root/writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]# crontab -l
*/1 * * * * /root/writeTime.sh

# 3. 查看定时任务执行的结果
[root@stream9 ~]# cat writeTime.sh
#!/bin/bash
date +%Y-%m-%d_%H_%M_%S >> time.log
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# cat time.log
2023-10-16_21_31_01
2023-10-16_21_32_02
2023-10-16_21_34_01
2023-10-16_21_35_01
2023-10-16_21_36_01
2023-10-16_21_37_01
2023-10-16_21_38_01
2023-10-16_21_39_01

# 3. 删除定时任务
[root@stream9 ~]# crontab -r -u root
[root@stream9 ~]#
[root@stream9 ~]# crontab -l
no crontab for root

3.清空文件
# 方法1


# 方法2
[root@stream9 ~]# ls
1.txt  2.txt  a.sh  time.log  writeTime.sh
[root@stream9 ~]# ls /dev/null
/dev/null
[root@stream9 ~]# echo 1111 > /dev/null
[root@stream9 ~]# echo 1111 >> /dev/null
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# cat /dev/null
[root@stream9 ~]#
[root@stream9 ~]# cat /dev/null > time.log #请空 .log 文件
[root@stream9 ~]#
[root@stream9 ~]# cat time.log
[root@stream9 ~]#

# 方法3  vim => 9999dd
4.获取文件的绝对路径 realpath
[root@stream9 ~]# ls
1.txt  2.txt  a.sh  time.log  writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]# realpath writeTime.sh
/root/writeTime.sh
5.给 脚本 加/减自动执行权限
[root@stream9 ~]# ls
1.txt  2.txt  a.sh  time.log  writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]# chmod +x writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]# ls
1.txt  2.txt  a.sh  time.log  writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]# ./writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# chmod -x writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# ls
1.txt  2.txt  a.sh  time.log  writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]# ./writeTime.sh
-bash: ./writeTime.sh: Permission denied
[root@stream9 ~]#
[root@stream9 ~]# bash writeTime.sh
6.more/less 查看文件
# cat 一次性全部输出 (输出到屏幕)
[root@vm1 ~]# cat 1.txt

# more 一次只铺满屏幕  space 换一页, nter 换一行(输出到屏幕)
[root@vm1 ~]# more 1.txt

# less 一次只铺满屏幕  space 换一页, nter 换一行(内部查看) q 推出
[root@vm1 ~]# less 1.txt
7.并且/或者
#!/bin/bash

# 方法1
[root@vm1 ~]# if [ $((1 + 2)) -gt 2 ] && [ $((2 + 1)) -gt 3 ]; then
   echo "3大于2 并且 4大于3"
else
   echo "上面有一条或多条为假"
fi
上面有一条或多条为假
[root@vm1 ~]#
[root@vm1 ~]#
[root@vm1 ~]# if [ $((1 + 2)) -gt 2 ] && [ $((2 + 2)) -gt 3 ]; then echo "3大于2 并且 4大于3"; else    echo "上面有一条或多条为假"; fi
3大于2 并且 4大于3

# 方法2
[root@vm1 ~]# if [[ $((1 + 2)) -gt 2 && $((2 + 1)) -gt 3 ]]; then
   echo "3大于2 并且 4大于3"
else
   echo "上面有一条或多条为假"
fi
上面有一条或多条为假
[root@vm1 ~]#
[root@vm1 ~]#
[root@vm1 ~]# if [[ $((1 + 2)) -gt 2 && $((2 + 2)) -gt 3 ]]; then    echo "3大于2 并且 4大于3"; else    echo "上面有一条或多条为假"; fi
3大于2 并且 4大于3

# 方法3 -a => and 并且, -o => or 或者 相当于 ||
# 用 -a 的地方,就能用 -o
[root@vm1 ~]# if [ $((1 + 2)) -gt 2 -a $((2 + 1)) -gt 3 ]; then
   echo "3大于2 并且 4大于3"
else
   echo "上面有一条或多条为假"
fi
上面有一条或多条为假
[root@vm1 ~]#
[root@vm1 ~]# if [ $((1 + 2)) -gt 2 -a $((2 + 2)) -gt 3 ]; then    echo "3大于2 并且 4大于3"; else    echo "上面有一条或多条为假"; fi
3大于2 并且 4大于3

# 方法4 && 的地方,就能用 ||
# [] 括号的地方,都可以改成 [[]], [[]] 的地方,不一定能改成 []
[root@vm1 ~]# if [[ $((1 + 2)) -gt 2 ]] && [[ $((2 + 1)) -gt 3 ]]; then
   echo "3大于2 并且 4大于3"
else
   echo "上面有一条或多条为假"
fi
上面有一条或多条为假
[root@vm1 ~]#
[root@vm1 ~]#
[root@vm1 ~]# if [[ $((1 + 2)) -gt 2 ]] && [[ $((2 + 2)) -gt 3 ]]; then    echo "3大于2 并且 4大于3"; else    echo "上面有一条或多条为假"; fi
3大于2 并且 4大于3

# 5. 举例 -o 的用法
[root@vm1 ~]# if [ $((1 + 2)) -gt 2 -o $((2 + 1)) -gt 3 ]; then
   echo "上面有一条为真或者都为真"
else
   echo "上面都为假"
fi
上面有一条为真或者都为真
[root@vm1 ~]#
[root@vm1 ~]# if [ $((1 + 2)) -gt 2 -o $((2 + 2)) -gt 3 ]; then echo "上面有一条为真或者都为真"; else echo "上面都为假"; fi
上面有一条为真或者都为真
[root@vm1 ~]#
[root@vm1 ~]# if [ $((0 + 2)) -gt 2 -o $((1 + 2)) -gt 3 ]; then echo "上面有一条为真或者都为真"; else echo "上面都为假"; fi
上面都为假
8.变量赋初始值
[root@vm1 ~]# pass=""
[root@vm1 ~]#
[root@vm1 ~]# pass=${pass:-123456}
[root@vm1 ~]#
[root@vm1 ~]# echo $pass
123456
[root@vm1 ~]#
[root@vm1 ~]# pass="111"
[root@vm1 ~]#
[root@vm1 ~]# pass=${pass:-123456}
[root@vm1 ~]#
[root@vm1 ~]# echo $pass
111
9.关闭/打开回显
#使用 stty -echo 关闭 shell 的回显功能
#使用 stty echo 打开 shell 的回显功能
stty -echo # 是可以单独执行的命令 不显示
stty echo  # 是可以单独执行的命令 显示

# 举例
[root@vm1 ~]# passwd
Changing password for user root.
New password:

10.最远/最近距离匹配
[root@vm1 ~]# str="root:x:0:0:root:/root:/bin/bash"
[root@vm1 ~]#
[root@vm1 ~]# echo $str
root:x:0:0:root:/root:/bin/bash
[root@vm1 ~]#
[root@vm1 ~]# echo ${str%:*}:abc # 最远距离匹配
root:x:0:0:root:/root:abc
[root@vm1 ~]#
[root@vm1 ~]# echo ${str%%:*}:abc # 最近距离匹配
root:abc
11.EOF 的使用 => 批量写入内容到文件
#!/bin/bash

function wirte_jb() {
   cat >test1.sh <<EOF
#!/bin/bash
for ((i = 1; i <= 5; i++)); do
    for ((j = 1; j <= i; j++)); do
        echo -n " *"
    done
    echo ""
done
for ((i=5; i>=1; i--));do
    for((j=1;j<=i;j++));do
        echo -n " *"
    done
    echo ""
done
EOF

   cat >test2.sh <<aaa
#!/bin/bash
for ((i = 1; i <= 5; i++)); do
    for ((j = 1; j <= i; j++)); do
        echo -n " *"
    done
    echo ""
done
for ((i=5; i>=1; i--));do
    for((j=1;j<=i;j++));do
        echo -n " *"
    done
    echo ""
done
aaa

# 要么就写绝对路径 常用绝对路径
   cat >/root/jb/test3.sh <<abc
1111
2222
3333
abc

   pwd
   ls
   chmod +x test1.sh
   ./test1.sh
}

wirte_jb

# [root@vm1 ~]# ls
# demo.sh
# [root@vm1 ~]# bash demo.sh
# /root
# demo.sh  test1.sh  test2.sh
#  *
#  * *
#  * * *
#  * * * *
#  * * * * *
#  * * * * *
#  * * * *
#  * * *
#  * *
#  *
# [root@vm1 ~]# ls
# demo.sh  test1.sh  test2.sh
posted on   鸟叔书  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示