linux shell入门基础

linuxlinux shell入门基础

Linux快捷键的使用

1.通过上下键↑↓来调用过往执行的linux命令
2.Tab可以补全想输入的命令
3. Ctrl + R 用于查找使用过的命令
4. Ctrl + L 用于清除屏幕并将当前行移到页面顶部
5. Ctrl + C 中止当前正在执行的命令
6. Ctrl + U 从光标位置剪切到行首
7. Ctrl + K 从光标位置剪切到行尾
8. Ctrl + W 剪切贯标左侧的一个单词
9. Ctrl + Y 粘贴Ctrl + U|K|W剪切的命令
10. Ctrl + D 关闭shell会话

bash基本命令的使用

所有的命令的使用可通过command --help查看用法
tr命令
Usage: tr [OPTION]… SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,
writing to standard output.

-c, -C, --complement use the complement of SET1
-d, --delete delete characters in SET1, do not translate
-s, --squeeze-repeats replace each sequence of a repeated character
that is listed in the last specified SET,
with a single occurrence of that character
-t, --truncate-set1 first truncate SET1 to length of SET2
–help display this help and exit
–version output version information and exit
作用:主要针对标准输入进行转换、压缩、去重、删除处理

  • -s参数:用set1中指定的字符集合去除标准输入中的重复字符
  • -t参数:必须指定set1和set2集合。将标准输入中set1集合的字符替换为对应位置的set2集合字符
    如果set1集合长度与set2集合长度相同,执行对应替换操作
    如果set1集合长度与set2集合长度不同,则按照最小长度替换,多余的字符舍弃
  • -c参数:用set2中的字符替换set1集合的补集字符
  • -d参数:将集合set1中的字符从输入中删除
  • 不加参数:默认执行-t操作,但是当set1和set2集合长度不同时,不同与指定-t参数
    如果set1集合长度大于set2集合长度,set2集合按照最后一个字符补齐到set1长度
    如果set1集合长度小于set2集合长度相同,截断set2集合长度使之与set1集合长度相同

示例:

$ echo "abcdaabbccdd123" | tr -s "abcd"
abcdabcd123
$ echo "aabbccdd123ff" | tr -t "abcd" "ABCDF"
AABBCCDD123ff
$ echo "aabbccdd123ff" | tr -t "abcd" "ABCDF"
AABBCCDD123ff
$ echo "aabbccdd123ff" | tr -t "abcdf" "ABCD"
AABBCCDD123ff
$ echo "aabbccdd123ff" | tr -c "abcdf" "#"
aabbccdd###ff#
$ echo "aabbccdd123ff" | tr -d "abcdf"
123
//执行默认参数操作
$ echo "aabbccdd123ff" | tr "abcdf" "ABCD"
AABBCCDD123DD
$ echo "aabbccdd123ff" | tr "abcd" "ABCDD"
AABBCCDD123ff

find命令
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path…] [expression]
作用:全局查找文件,并执行特定的命令
示例:

//全局准确查找test.txt,不是查找包含test.txt关键字---与locate的区别
//默认的path为当前目录,默认的expression为-print
//1、根据文件名查找
$ find -name "test.txt"
./test.txt
//2、根据文件大小查找--查找当前目录下小于10个字符的文件
find ./ -size -10c -type f
./20180827/person/backup/xxx_20181017/data/mysql/columns_priv.MYD
//3、根据访问时间查找--查找当前目录下7天内访问且后缀为.txt的文件
$ find ./ -name "*.txt" -atime -7
./test.txt
//3、操作查找结果--test.txt中的内容为abcd-
//-ok类似于-exec,只是需要确认
$ find -atime -7 -name "*.txt" -printf "%p - %u\n"
./test.txt - Administrator
$ find -atime -7 -name "*.txt" -exec cat {} \;
abcd
$ find -atime -7 -name "*.txt" -ok cat {} \;
< cat ... ./test.txt > ? y
abcd

sort命令
Usage: sort [OPTION]… [FILE]…
or: sort [OPTION]… --files0-from=F
Write sorted concatenation of all FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
作用:对输入的内容进行排序,打印到标准输出中

  • -n 参数:对数字进行排序,默认将数据以字符形式排序,加上-n参数后,将按照数字大小排序
  • -r参数:按照倒序排序
  • -R参数:随机排序
  • -o参数:将排序后的我呢间写入到新文件中 -o后面加新文件的名字

示例:

//test.txt文本内容
$ cat test.txt
abcd
123
defg
zhl
hai
lin
35
//执行sort命令,默认是升序
$ sort test.txt
123
35
abcd
defg
hai
lin
zhl
//执行-r参数的倒序
$ sort -r test.txt
zhl
lin
hai
defg
abcd
35
123
//执行-R参数的随机排序
$ sort -R test.txt
123
lin
hai
abcd
35
defg
zhl
//执行-o参数的输出到新文件
$ sort -o test-new.txt test.txt
$ cat test-new.txt
123
35
abcd
defg
hai
lin
zhl

wc命令
Usage: wc [OPTION]… [FILE]…
or: wc [OPTION]… --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. A word is a non-zero-length sequence of
characters delimited by white space.
作用:统计文件的行数,单词个数及字节数

  • -l 参数:打印文件的行数
  • -w参数:打印文件的单词书
  • -c参数:打印文件的字节数
  • -m参数:打印这个字符数

示例:

//查看test.txt文件信息
$ ls -l test.txt
-rw-r--r-- 1 Administrator 197121 29 Apr 11 16:13 test.txt
//执行wc命令,结果结息为:行数、单词的个数以及字节数	
$ wc test.txt
 7  7 29 test.txt
//打印文件字节数
$ wc -c test.txt
29 test.txt
//打印文件字符个数
$ wc -m test.txt
29 test.txt
//打印文件行数
$ wc -l test.txt
7 test.txt

uniq命令
Usage: uniq [OPTION]… [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).
With no options, matching lines are merged to the first occurrence.
作用:删除文件中的重复内容
注:uniq是对连续重复行数进行去重,所以为保证效果,必须联合sort命令一起使用

  • -c 参数:统计每一行的情况
  • -d 参数:只显示重复的行
  • -u 参数:仅仅打印不重复的行
  • 无参数:打印去重后的行

示例:

//查看test.txt文件内容
$ cat test.txt
abcd
123
defg
zhl
hai
lin
35
123
lin
//排序后执行uniq默认参数命令,打印去重后的行
$ sort test.txt | uniq
123
35
abcd
defg
hai
lin
zhl
//仅打印唯一行
$ sort test.txt | uniq -u
35
abcd
defg
hai
zhl
//仅打印重复行
$ sort test.txt | uniq -d
123
lin
//执行wc命令,结果结息为:行数、单词的个数以及字节数	
$ sort test.txt | uniq -c
      2 123
      1 35
      1 abcd
      1 defg
      1 hai
      2 lin
      1 zhl

cut命令
Usage: cut OPTION… [FILE]…
Print selected parts of lines from each FILE to standard output.
With no FILE, or when FILE is -, read standard input.
作用:剪切文件的内容

  • -d 参数:设置文件每一行的分割符。默认分隔符为空格
  • -f参数:选择获取的每一行内容的fields。常与-d参数连用
  • -c参数:设置每一行内容的字符数

示例:

//查看测试数据信息
$ cat test-new.txt
123,456,789
35,46,23
abcd,ab,cd
defg,de,fg
hai,ha,ai
lin,li,in
zhl,zh,hl
//执行cut命令,获取每一行的第2-4个字符	
$ cut -c 2-4 test-new.txt
23,
5,4
bcd
efg
ai,
in,
hl,
//设置文件的分隔符并选取需要的fields
$ cut -d ',' -f 1-2 test-new.txt
123,456
35,46
abcd,ab
defg,de
hai,ha
lin,li
zhl,zh

tar命令
Usage: tar [OPTION…] [FILE]…
GNU ‘tar’ saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.
tar -tvf archive.tar # List all files in archive.tar verbosely.
tar -xf archive.tar # Extract all files from archive.tar.
作用:归档、压缩、解压文档

  • -c --create参数:创建归档文件
  • -d --diff --compare参数:比较这个归档文件和某些文件的不同
  • -r, --append参数:添加文件到归档文件总。不检查是否归档文件中已存在
  • -t, --list参数:显示归档文件中的文件名称
  • -u, --update参数:比较将新的文件copy到归档文件中
  • -x, --extract参数:解除归档。已有的.tar文件并不会删除
  • -v, --verbose参数:执行命令时打印冗余信息
    Compression options:
  • -j, --bzip2:使用bzip2进行压缩、解压缩
  • -J, --xz:使用xz进行压缩、解压缩
  • -z, --gzip:使用gzip进行压缩、解压缩

示例:

//创建归档文件test.tar
$ tar -cvf test.tar test.txt test-new.txt
test.txt
test-new.txt
//查看test.tar的归档文件	
$ tar -tf test.tar
test.txt
test-new.txt
$ tar -tvf test.tar
-rw-r--r-- Administrator/197121 37 2021-04-11 16:29 test.txt
-rw-r--r-- Administrator/197121 80 2021-04-11 16:48 test-new.txt
//添加test1.txt到test.tar归档中
$ tar -rf test.tar test1.txt
$ tar -tvf test.tar
-rw-r--r-- Administrator/197121 37 2021-04-11 16:29 test.txt
-rw-r--r-- Administrator/197121 80 2021-04-11 16:48 test-new.txt
-rw-r--r-- Administrator/197121  0 2021-04-11 17:13 test1.txt
//因为test.tar中已经存在了test1.txt,所以执行-u命令不会重复添加
$ tar -uvf test.tar test1.txt
$ tar -tvf test.tar
-rw-r--r-- Administrator/197121 37 2021-04-11 16:29 test.txt
-rw-r--r-- Administrator/197121 80 2021-04-11 16:48 test-new.txt
-rw-r--r-- Administrator/197121  0 2021-04-11 17:13 test1.txt
-rw-r--r-- Administrator/197121  0 2021-04-11 17:13 test1.txt
//解除归档文件
$ tar -xvf test.tar
test.txt
test-new.txt
test1.txt
test1.txt
//创建归档文件并压缩
$ tar -czvf test.tar.gz test.txt test-new.txt
test.txt
test-new.txt
//解压缩并解除归档
$ tar -xzvf test.tar.gz
test.txt
test-new.txt

tee命令
Usage: tee [OPTION]… [FILE]…
Copy standard input to each FILE, and also to standard output.
作用:读取标准输入的数据,并将其内容输出成文件

  • -a, --append参数: 追加内容到某文件中,不是覆盖
  • -i, --ignore-interrupts参数:忽略中断信号
$ cat test.txt | tee zhl.log
abcd
123
defg
zhl
hai
lin
35
123
lin

$ cat zhl.log
abcd
123
defg
zhl
hai
lin
35
123
lin
//执行-a追加命令
$ cat test.txt | tee -a zhl.log
abcd
123
defg
zhl
hai
lin
35
123
lin

$ cat zhl.log
abcd
123
defg
zhl
hai
lin
35
123
lin
abcd
123
defg
zhl
hai
lin
35
123
lin

systemctl命令
管理后台守护进程的相关命令

systemctl start sshd		//启动sshd服务
systemctl stop sshd			//停止sshd服务
systemctl restart sshd		//重启sshd服务
systemctl status sshd		//查看sshd服务状态
systemctl reload sshd		//重载sshd服务配置文件(服务不停止)
systemctl enable sshd		//设置开机自动启动sshd服务
systemctl disable sshd		//设置开机不自动启动sshd服务
systemctl is-enabled sshd	//查看sshd服务是否开机自启动
systemctl list-unit-files --type=service	//查看各个级别下服务的启动和禁用情况

ssh命令
远程连接服务器
常用的命令:ssh user@ip:port
例如: ssh root@192.168.10.134:22 可远程登录192.168.10.134,需输入密码
ssh的相关配置文件:

  • 全局的ssh服务端的配置:/etc/ssh/sshd_config文件
  • 全局的ssh客户端的配置:/etc/ssh/ssh_config文件
  • 当前用户ssh客户端的配置:~/.ssh/config
    修改配置后需要手动重启服务:systemctl restart sshd

wget命令
从服务器下载文件
usage:wget [参数][URL地址]
例如: wget http://www.xxx.com/xxx.zip

scp命令
利用网路在两个机器间拷贝数据
scp [-346ABCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file] [-J destination] [-l limit] [-o ssh_option] [-P port] [-S program] source … target
例如:
scp ./test.txt root@192.168.0.9:/log/tmp/ 将本地的test.txt拷贝到远程机器上的/log/tmp目录下
scp root@192.168.0.9:/log/tmp/test.txt ./ 将远程机器上的/log/tmp目录下的test.txt拷贝到当前目录下

top命令详解
https://www.jianshu.com/p/3f19d4fc4538

bash脚本

bash脚本的调试
脚本调试阶段,应该设置以下几个参数

  • -x:在执行每条命令时,进行变量的替换,并将整个命令打印出来
  • -n:读取整个脚本的命令,但不执行,用于检查脚本错误
  • -v:一边执行脚本,一边将执行过的脚本命令打印到标准输出
  • -e:命令执行失败(返回码不为0),立即退出脚本
  • -u:试图使用未定义的变量,立即退出。bash中会将未定义的变量替换为空串
  • -o pipefail:在使用连续管道时,只要管道中的一个子命令失败,迮整个管道命令失败。(一般情况下只有最后一个管道失败,则整个管道的错误码返回才是非0)

设置方式:
1、在脚本中设置:-表示开启;+表示关闭
set -xv 开启xv参数,set +xv关闭xv参数
2、在调用脚本时制定
sh -xv ./test.sh

//展示脚本neir ong
$ cat test.sh
#!/bin/bash
set -x
TEST_DIR=$(pwd)
echo ${TEST}
echo ${TEST_DIR}
//执行指定-x的sh脚本
$ bash test.sh
++ pwd
+ TEST_DIR=/e
+ echo

+ echo /e
/e
//执行指定-v的sh脚本
$ bash test.sh
TEST_DIR=$(pwd)
echo ${TEST}

echo ${TEST_DIR}
/e
//启用-u参数
$ sh -uvx test.sh
#!/bin/bash
set -v
+ set -v
TEST_DIR=$(pwd)
++ pwd
+ TEST_DIR=/e
echo ${TEST}
test.sh: line 4: TEST: unbound variable
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值