一. 输入输出重定向
回顾1 输入输出重定向
read -p “请输入” a -p 提示 a 赋值给a
read -s 密码隐藏
[root@sanchuang-linux chenpeng]# cat >here_test.txt <<eof< code><br>> nihao<br>> sanchuang<br>> huanying<br>> world............<br>> x y z<br><code>\> EOF</code><br>[root@sanchuang-linux chenpeng]# cat here_test.txt<br>nihao<br>sanchuang<br>huanying<br>world............<br>x y z</eof<>
知识点3.2 EOF是文档结束标志 可以自行定义 (end of file)
示例:
[root@sanchuang-linux chenpeng]# echo “aa” >test_aa.txt #(注:默认不输出到屏幕)
[root@sanchuang-linux chenpeng]# cat test_aa.txt
aa
[root@sanchuang-linux chenpeng]# echo "bb" |tee test_bb.txt #
(注:屏幕+文件)
bb
[root@sanchuang-linux chenpeng]# cat test_bb.txt
bb
五. 清空文件内容
知识点5 清空文件内容
[root@sanchuang-linux chenpeng]# >test_bb.txt
[root@sanchuang-linux chenpeng]# echo > test_bb.txt
#(注:有换行)
[root@sanchuang-linux chenpeng]# cat test_bb.txt
[root@sanchuang-linux chenpeng]# echo -n > test_bb.txt
[root@sanchuang-linux chenpeng]# cat test_bb.txt
[root@sanchuang-linux chenpeng]# :>test_bb.txt
[root@sanchuang-linux chenpeng]# cat test_bb.txt
知识点6 echo
echo
在屏幕上显示一段文本或指定内容。
[En]
To display a paragraph of text or specified content on the screen.
输出变量,输出指定内容
-e 选项 转义字符输出
-n 选项 不接换行
六. SHELL入门
shell入门
shell 是一个用C语言写的程序,它是用户使用linux的桥梁
shell 脚本 实现自动化 重复性的操作编写脚本完成,减少人工失误
shell的变量
1、局部变量 定义在脚本或命令中
2、环境变量 shell启动的程序能访问到的环境变量 env、 echo $PATH
3、shell变量
示例:环境变量
a=1
echo $a
echo ${a}
知识点8.2 变量名命名规则
变量名命名规则:
它是数字、字母和下划线的组合,不能以数字开头。
[En]
It is a combination of numbers, letters, and underscores, and cannot start with a number.
不能使用bash中的关键字
使用一个定义过的变量,需要在前面加上$符号
示例:
[root@sanchuang-linux ~]# python3 canshu.py "hello" "world"
hello world
canshu.py
知识点11 数据类型
shell常用数字、字符串、数组
字符串的定义可以用单引号、双引号或不带引号
[En]
The definition of a string can be in single quotation marks, double quotation marks or without quotation marks
示例:字符串的定义__________________
[root@sanchuang-linux ~]# echo abc
abc
[root@sanchuang-linux ~]# a=b
[root@sanchuang-linux ~]# echo $a
b
[root@sanchuang-linux ~]# a="b"
[root@sanchuang-linux ~]# echo $a
b
[root@sanchuang-linux ~]# a='b'
[root@sanchuang-linux ~]# echo $a
b
示例:数字的定义_________________
[root@sanchuang-linux ~]# a=1
[root@sanchuang-linux ~]# a=2
知识点12 引号区别:双引号可以识别变量,单引号不可以识别变量
引号之间的区别:双引号可以标识变量,但不能标识单引号。
[En]
The difference between quotation marks: double quotes can identify variables, but not single quotation marks.
[root@sanchuang-linux ~]# head -n1 /etc/passwd #(注:输出passwd第一条)
root:x:0:0:root:/root:/bin/bash
[root@sanchuang-linux ~]# cat /etc/passwd |head -n1 #(注:不建议用这个 2条命令)
root:x:0:0:root:/root:/bin/bash
#!/bin/bash
字符串操作
line=head -n1 /etc/passwd
#(注:使用反引号
)(注:把命令输出保存在line里面)
echo $line
echo '字符串为:$line'
字符串为:$line
知识点13 字符串操作
截取
截取前4个字符:echo ${line:0:4}
截取后9个字符 echo ${line:0-9}
从倒数第九个字符开始截取4个字符 echo ${line:0-9:4}
从左向右截取最后一个:后的字符 echo ${line##*:}
从左向右截取第一个:后的字符 echo ${line#*:}
从右往左截取最后一个:后的字符 echo ${line%%:*}
从右向左截取第一个:后的字符 echo ${line%:*}
字符串长度 echo ${#line}
示例:字符串操作_______________________________
字符串操作
[root@sanchuang-linux chenpeng]# vim test2.sh
line=head -n1 /etc/passwd
echo $line #(注:root:x:0:0:root:/root:/bin/bash)
echo "字符串为:$line" #(注:字符串为:root:x:0:0:root:/root:/bin/bash)
echo '字符串为:$line' #(注:字符串为:$line)
echo "截取前4个字符:"
echo ${line:0:4} #(注:root)
echo "截取后9个字符"
echo ${line:0-9} #(注:/bin/bash)
echo "从倒数第九个字符开始截取4个字符"
echo ${line:0-9:4} #(注:/bin)
echo "从左向右截取最后一个:后的字符"
echo ${line##*:} #(注:/bin/bash)
echo "从左向右截取第一个:后的字符"
echo ${line#*:} #(注:x:0:0:root:/root:/bin/bash)
echo "从右往左截取最后一个:后的字符"
echo ${line%%:*} #(注:root)
echo "从右向左截取第一个:后的字符"
echo ${line%:*} #(注:root:x:0:0:root:/root)
echo "字符串长度"
echo ${#line} #(注:31)
for 变量 in 值1 值2
do
循环执行语句
done
=======================================
语法2:↓↓↓↓↓↓↓
while read line
do
循环执行语句
done
=======================================
语法2↓↓↓↓↓↓↓↓↓
if 条件
then
执行语句
fi
=========================
语法2:↓↓↓↓↓
if 条件
then
执行语句
else
执行语句
fi
==========================
语法3:↓↓↓↓↓↓
整成函数形式
add(){
echo "两数相加为:$(($num1 + $num2))"
}
case $options in
1)
add #(注:需要使用的时候调用)
;;
2)..............................
知识点17 /etc/init.d 服务的启动脚本
/etc/init.d/ 放着服务的启动脚本
[root@sanchuang-linux chenpeng]# cd /etc/init.d/
[root@sanchuang-linux init.d]# ls
functions README
示例:服务重启脚本用case↓↓↓↓↓__________________
case $mode in
start)
启动
;;
stop)
关闭(使用kill命令)
;;
restart)
关闭
启动
;;
reload)
重新加载配置(使用kill -HUP)
;;
esac
知识点18 kill
kill 用来删除正在执行中的程序或者工作
kill 可以将指定的信息发送给程序
kill -l 可以查看kill信号量 (kill -L(小写))
kill -0 用来检测进程是否存在,当进程不存在时,kill -0 会报错
kill -1 pid 重新加载进程(常用)
kill -HUP pid 和 kill -1 pid是一样的
kill -1 pid 或者 kill -HUP pid 都表示重新加载这个文件
kill -9 强制杀死
kill -15 正常停止一个进程
kill 不接信号量的时候,默认为信号15
除了9号信号,其他信号进程都有权利拒绝执行!
注:重新加载 相当于 加载最新的配置 服务还是正常运行的(连接不会断)
重启 服务会断
示例:↓↓↓↓↓↓↓↓↓____________
[root@sanchuang-linux ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
..............................
63) SIGRTMAX-1 64) SIGRTMAX
知识点19 shell编程 if判断
if 判断
示例:↓↓↓↓↓↓____________________________________________________________
[root@sanchuang-linux ~]# if id wenyao; then echo "ok"; else echo "error"; fi
id: "wenyao":无此用户
error
写法2:推荐(类似python的if三元运算符)
[ -f a.txt ] && echo "文件已存在" || touch a.txt
示例2:
编写脚本以执行以下操作
[En]
Write a script to do the following
==============
1.增加用户并设置密码
2.删除用户
3.查看用户
4.退出
==============
输入的指定不是1-4,给提示给予提醒,并且如果不输入退出的话,可以循环添加。
按1 增加用户,并且设置密码 useradd passwd
按2 删除用户 userdel -r
按3 查看用户 id
按4 退出 exit
知识点22 类似python的if三元运算符
使用&& || 来实现
·cmd1 && cmd2 如果cmd1执行成 功,或者为真,则执行cmd2
·cmd1 || cmd2 如果cmd1执行不成功,或者为假,则执行cmd2
·cmd1 && cmd2 || cmd3 如果cmd1执行成功,就执行cmd2,不成功就执行cmd3
示例:上一个练习↓<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Example: previous exercise ↓</font>*</details>
[ -f a.txt ] && echo "文件已存在" || touch a.txt
[[ -f a.txt ]] && echo "文件已存在" || touch a.txt #(注:推荐使用两个中括号)
示例:
[root@sanchuang-linux chenpeng]# name="wen yao"
[root@sanchuang-linux chenpeng]# [ $name == "wen yao" ] && echo "ok" || echo "error"
-bash: [: 参数太多 #(注:自动做单词拆分)
error
[root@sanchuang-linux chenpeng]# [[ $name == "wen yao" ]] && echo "ok" || echo "error"
ok #(注:推荐使用2个中括号)
[root@sanchuang-linux chenpeng]# [ "$name" == "wen yao" ] && echo "ok" || echo "error"
ok #(注:使用引号连接在一起,表示一个整体)
============================================================================================
示例2:数值比较
[root@mysql-binary shell_test]# a=10
[root@mysql-binary shell_test]# b=20
[root@mysql-binary shell_test]# [[ $a -eq $b ]] && echo "ok" || echo "eroor"
eroor
[root@mysql-binary shell_test]# (( $a -eq $b )) && echo "ok" || echo "eroor"
-bash: ((: 10 -eq 20 : 表达式中有语法错误 (错误符号是 "20 ")
Eroor
[root@sanchuang-linux ~]# a=10
[root@sanchuang-linux ~]# b=20
[root@sanchuang-linux ~]# if [[ $a > $b ]]; then echo "ok"; else echo "error"; fi
error
[root@sanchuang-linux ~]# [[ $a > $b ]] && echo "ok" || echo "error"
error
示例:字符串比较(( )) 也可以
调用的时候 add
case $options in
1)
add
;;
2)........................
[root@sanchuang-linux ~]# a=123
[root@sanchuang-linux ~]# b=123
[root@sanchuang-linux ~]# test a==b && echo ok
ok
[root@sanchuang-linux ~]# test a==b && echo ok || echo error
ok
Original: https://www.cnblogs.com/mycpen/p/16650976.html
Author: mycpen
Title: 10_Linux基础-SHELL入门1
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/522759/
转载文章受原作者版权保护。转载请注明原作者出处!