i.MX6ULL 学习进阶
Linux文件目录
用户管理与文件权限
使用Linux命令行
对外接受用户输入的命令。
对内通过系统调用传递给内核。
呈现内核运行结果。
查询命令
- man
目录操作类
cd:
pwd:
mkdir:
rmdir:
mv:
文本操作类
touch:
cat:
echo:
wc:
rm:
ln:
cp:
tar:
find:
grep:
用户管理类
useradd/adduser:
usermod:
userdel/deluser:
passwd:
groupadd/addgroup:
groupdel/delgroup:
su:
操作权限类
sudo
chmod
chown
chgrp
磁盘管理类
df:
du:
mount:
umount:
网络操作类
ifconfig
ping
控制终端类
- clear:
开关机命令
reboot
poweroff
编辑器
gedit编译器
简单易懂,依赖图形界面
vi/vim编辑器
为什么要学习vi/vim编辑器
vi与vim的区别:
- vim是vi的升级版本,兼容vi
- vi按 u只能撤消上次命令,而在vim里可以无限制的撤消
- vi只能运行于类unix系统中,而vim不仅可以运行于unix,windows ,mac等多操作平台
- vim可以用不同的颜色来高亮代码
- 可通过vimrc文件配置更加高级的功能
vi/vim使用
安装:
sudo apt install vim
vi/vim的三种模式
- 一般模式(默认模式)
- 插入模式(编辑模式)
- 命令行模式
打开Vi/Vim
vi 文件名
vim 文件名
进入/退出插入模式快捷键
快捷键 | 功能描述 |
---|---|
i | 在当前光标所在位置插入文本 |
a | 在当前光标所在位置的下一个字符插入文本 |
o | 在光标所在位置后插入新行 |
r | 替换当前光标所在位置的字符 |
R | 可以替换当前光标所在位置之后的字符,按下”Esc”则退出 |
ESC | 退出插入模式 |
一般模式快捷键
快捷键 | 功能描述 | |
---|---|---|
光标移动 | k / ↑ | 光标向上移动 |
j / ↓ | 光标向下移动 | |
h / ← | 光标向左移动 | |
l / → | 光标向右移动 | |
PageUp | 向上翻页 | |
PageDown | 向下翻页 | |
n+shift+g | 跳转到第n行 | |
shift+g | 跳转到最末行 | |
gg | 跳转到第一行 | |
文本查找与替换 | /word | 在文件中搜索关键字word |
n | 查找下一个关键字 | |
N | 查找上一个关键字 | |
撤销重做 | u | 撤销上一步的操作,等价于Windows的Ctrl+Z |
Ctrl+r | 重做上一步的操作。 | |
删除、剪切、复制、粘贴 | dw | 删除一个单词 |
dd | 删除当前行 | |
ndd | 删除光标后n行 | |
x | 剪切光标选中的字符 | |
y | 复制光标所选的内容 | |
yy | 复制当前行 | |
nyy | 复制当前行后n行 | |
p | 将复制的数据粘贴在当前行的下一行 | |
P | 将复制的数据粘贴在当前行的上一行 | |
区块操作 | v | 选择多个字符 |
V | 可以选择多行 |
命令行模式快捷键
快捷键 | 功能描述 |
---|---|
w | 保存文档 |
w |
另存为以 |
r |
读取文件名为filename的文档 |
q | 直接退出软件,前提是文档未做任何修改 |
q! | 不保存修改,直接退出软件 |
wq | 保存文档,并退出软件。 |
set nu | 在行首加入行号 |
set nonu | 不显示行号 |
set hlsearch | 搜索结果高亮显示 |
! command | 回到终端窗口,执行command命令,按回车键可切回vim。 |
Shell脚本编程
Shell脚本是什么?
- shell命令按一定语法组成的文件
Shell脚本有什么用?
批处理文件/整合命令
- 软件启动
- 性能监控
- 日志分析
…
Shell命令的本质
内置命令/外部命令
外部命令从下面的路径寻找:
debian@npi:/etc$ echo $PATH
/home/debian/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
Shell脚本语言和C语言一样吗?
- 编译型语言
- 解释型语言
常用的Shell解释器有哪些?
cat /etc/shells
root@R9000X-Jungle:/etc# cat shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/sh
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
第一个Shell脚本
helloworld
编辑、保存、改权限、运行/排错
Shell启动方式
当程序执行
指定解释器运行
source和.
root@npi:~# chmod 777 hello.sh root@npi:~# ./hello.sh hello, jungle root@npi:~# source hello.sh hello, jungle root@npi:~# . hello.sh hello, jungle
Shell脚本语法讲解
定义变量
variable=value
variable=’value’
variable=”value”
root@npi:/home/debian# cat test.sh #!bin/bash var=1234 echo "$var"
使用变量
$variable
${variable}
root@npi:/home/debian# cat test.sh #!bin/bash var=1234 echo "$var" var1="123 456" var2="${var1}a" echo "${var2}bb"
将命令的结果赋值给变量
variable=`command`
variable=$(command)
debian@npi:~$ cat ./test.sh #!bin/bash var=1234 echo "$var" var1="123 456" var2="${var1}a" echo "${var2}bb" var3=`pwd` echo "${var3}" var4=$(pwd) echo "${var4}" debian@npi:~$ sh ./test.sh 1234 123 456abb /home/debian /home/debian
删除变量
- unset
特殊变量
变量 | 含义 |
---|---|
$0 | 当前脚本的文件名。 |
$n(n≥1) | 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是 $1,第二个参数是 $2。 |
$# | 传递给脚本或函数的参数个数。 |
$* | 传递给脚本或函数的所有参数。 |
$@ | 传递给脚本或函数的所有参数。当被双引号" " 包含时,$@ 与 $* 稍有不同. |
$? | 上个命令的退出状态或者获取函数返回值。 |
$$ | 当前 Shell 进程 ID。对于 Shell 脚本,就是这些脚本所在的进程 ID。 |
读取从键盘输入的数据
read
debian@npi:~$ ./test.sh
10
20
10+20=30
debian@npi:~$ cat ./test.sh
read a
read b
c=$(echo "$a + $b" | bc) # 支持浮点数计算
debian@npi:~$ ./test.sh
input a:12
input b:62
12+62=74
debian@npi:~$ cat ./test.sh
read -p "input a:" a
read -p "input b:" b
c=$(echo "$a + $b" | bc) # 支持浮点数计算
echo "${a}+${b}=${c}"debian@npi:~$
注:如果需要处理浮点数(小数),$((...))
无法支持,此时可以使用 bc
工具
退出当前进程
exit
对整数进行数学运算
(())
debian@npi:~$ ./test.sh
input a:1
input b:2
1+2=3
debian@npi:~$ cat ./test.sh
read -p "input a:" a
read -p "input b:" b
c=$(( $a+$b ))
逻辑与/或
AND
command1 为 false 则 直接 为 false
command1 && command2
OR
command1 为 true 则 直接 为 true
command1 || command2
检测某个条件是否成立
test expression和[ expression ]
选 项 | 作 用 |
---|---|
-eq | 判断数值是否相等 |
-ne | 判断数值是否不相等 |
-gt | 判断数值是否大于 |
-lt | 判断数值是否小于 |
-ge | 判断数值是否大于等于 |
-le | 判断数值是否小于到等于 |
-z str | 判断字符串 str 是否为空 |
-n str | 判断字符串str是否为非空 |
=和== | 判断字符串str是否相等 |
-d filename | 判断文件是否存在,并且是否为目录文件。 |
-f filename | 判断文件是否存在,井且是否为普通文件。 |
test expression
debian@npi:~$ ./test.sh
input a:1
input b:2
a!=b
a<b
a<=b
debian@npi:~$ cat ./test.sh
read -p "input a:" a
read -p "input b:" b
test ${a} -eq ${b} && echo "a==b"
test ${a} -ne ${b} && echo "a!=b"
test ${a} -lt ${b} && echo "a<b"
test ${a} -gt ${b} && echo "a>b"
test ${a} -le ${b} && echo "a<=b"
debian@npi:~$
[ expression ]
debian@npi:~$ ./test.sh
input a:2
input b:3
a!=b
a<b
a<=b
debian@npi:~$ cat ./test.sh
read -p "input a:" a
read -p "input b:" b
[ ${a} -eq ${b} ] && echo "a==b"
[ ${a} -ne ${b} ] && echo "a!=b"
[ ${a} -lt ${b} ] && echo "a<b"
[ ${a} -gt ${b} ] && echo "a>b"
[ ${a} -le ${b} ] && echo "a<=b"
str
debian@npi:~$ ./test.sh
input a string:eee
not null
debian@npi:~$ cat ./test.sh
read -p "input a string:" c
[ -z "$c" ] && echo "is null" || echo "not null"
debian@npi:~$ ./test.sh
input a string:
is null
debian@npi:~$
-d
-f
debian@npi:~$ ./test.sh
input a string:/home
/home not null
/home is dir
/home not file
debian@npi:~$ cat ./test.sh
read -p "input a string:" c
[ -z "$c" ] && echo "${c} is null" || echo "${c} not null"
[ -d "$c" ] && echo "${c} is dir" || echo "${c} not dir"
[ -f "$c" ] && echo "${c} is file" || echo "${c} not file"
管道
command1 | command2
debian@npi:~$ ls | grep test.sh
test.sh
debian@npi:~$ cat test.sh | grep "$c"
read -p "input a string:" c
[ -z "$c" ] && echo "${c} is null" || echo "${c} not null"
[ -d "$c" ] && echo "${c} is dir" || echo "${c} not dir"
[ -f "$c" ] && echo "${c} is file" || echo "${c} not file"
if语句
if condition
then
statement(s)
fi
if else 语句
if condition
then
statement1
else
statement2
fi
if elif else 语句
if condition1
then
statement1
elif condition2
then
statement2……
else
statementn
fi
debian@npi:~$ ./test.sh
input a:1
input b:2
input c:3
3 is the largest
debian@npi:~$ cat ./test.sh
read -p "input a:" a
read -p "input b:" b
read -p "input c:" c
if [ $a -gt $b ] && [ $a -gt $c ]; then
echo "$a is the largest"
elif [ $b -gt $a ] && [ $b -gt $c ]; then
echo "$b is the largest"
else
echo "$c is the largest"
fidebian@npi:~$
case in语句
case expression in
pattern1)
statement1
;;
pattern2)
statement2
;;
pattern3)
statement3
;;
……
*)
statementn
esac
debian@npi:~$ cat ./test.sh
#!/bin/bash
read -p "input a:" a
read -p "input b:" b
read -p "input c:" c
case $(($a > $b && $a > $c))$(($b > $a && $b > $c)) in
10)
echo "$a is the largest"
;;
01)
echo "$b is the largest"
;;
*)
echo "$c is the largest"
;;
debian@npi:~$ ./test.sh
input a:1
input b:2
input c:3
3 is the largest
代码解释
输入部分:通过
read
命令分别获取用户输入的三个值a
、b
和c
。case in
语句:
$(($a > $b && $a > $c))
和$(($b > $a && $b > $c))
会计算两个布尔表达式的值,结果为0
或1
。- 将这两个结果拼接起来作为
case
语句的匹配条件。 - 如果拼接结果为
10
,表示a
是最大值。 - 如果拼接结果为
01
,表示b
是最大值。 - 其他情况(即
a
和b
都不是最大值),则c
是最大值。
**
;;
**:用于结束每个case
分支。
for in 循环
for variable in value_list
do
statements
done
value_list
- 直接给出具体的值
- 给出一个取值范围
- 使用命令的执行结果
- 使用 Shell 通配符
- 使用特殊变量
debian@npi:~$ cat ./test.sh
#!/bin/bash
# 提示用户输入三个数字
read -p "input a:" a
read -p "input b:" b
read -p "input c:" c
# 将输入的数字存储在数组中
numbers=($a $b $c)
# 假设数组的第一个元素是最大值
max=${numbers[0]}
# 使用 for 循环遍历数组
for num in "${numbers[@]}"
do
# 如果当前元素大于最大值,则更新最大值
if [ $num -gt $max ]; then
max=$num
fi
done
# 输出最大值
echo "$max is the largest"debian@npi:~$ ./test.sh
input a:1
input b:2
input c:3
3 is the largest
debian@npi:~$
代码解释:
- 用户输入:使用
read
命令提示用户输入三个数字a
、b
和c
。 - 数组存储:将输入的三个数字存储在
numbers
数组中。 - 初始化最大值:假设数组的第一个元素是最大值,将其赋值给变量
max
。 for
循环:使用for
循环遍历数组中的每个元素。- 比较更新:在循环中,使用
if
语句比较当前元素和最大值,如果当前元素大于最大值,则更新最大值。 - 输出结果:循环结束后,输出最大值。
区别总结
- 来源不同:
${numbers[@]}
引用的是用户自定义数组中的元素,而$@
引用的是传递给脚本或函数的参数。 - 用途不同:
${numbers[@]}
用于处理自定义数组,而$@
用于处理命令行参数或函数参数
while 循环
while condition
do
statements
done
debian@npi:~$ ./test.sh
input a:1
input b:2
input c:3
3 is the largest
debian@npi:~$ cat ./test.sh
#!/bin/bash
# 提示用户输入三个数字
read -p "input a:" a
read -p "input b:" b
read -p "input c:" c
# 将输入的数字存储在数组中
numbers=($a $b $c)
# 假设数组的第一个元素是最大值
max=${numbers[0]}
# 初始化索引
index=1
# 使用 while 循环遍历数组
while [ $index -lt ${#numbers[@]} ]; do
if [ ${numbers[$index]} -gt $max ]; then
max=${numbers[$index]}
fi
# 索引加 1
((index++))
done
# 输出最大值
echo "$max is the largest"debian@npi:~$
代码解释:
- 输入部分:通过
read
命令提示用户输入三个数字,并将它们存储在数组numbers
中。 - 初始化最大值:假设数组的第一个元素是最大值,将其赋值给变量
max
。 - 初始化索引:设置一个索引变量
index
为 1,因为我们已经将第一个元素作为初始最大值。 - while 循环:使用
while
循环遍历数组,只要index
小于数组的长度,就继续循环。 - 比较和更新最大值:在循环中,使用
if
语句比较当前元素和max
的大小,如果当前元素大于max
,则更新max
的值。 - 索引递增:每次循环结束后,将
index
加 1,以便继续遍历下一个元素。 - 输出结果:循环结束后,输出最大值。
总结
${#numbers[@]}
用于获取数组中元素的数量,它是针对数组变量的操作。$#
用于获取传递给脚本的命令行参数的数量,它是一个特殊的脚本变量。
函数
function name() {
statements
[return value]
}
debian@npi:~$ sh ./test.sh
input a:1
input b:2
input c:3
3 is the largest
debian@npi:~$ cat ./test.sh
#!/bin/bash
# 定义函数来找出最大值
find_largest() {
local a=$1
local b=$2
local c=$3
if [ $a -gt $b ] && [ $a -gt $c ]; then
echo "$a is the largest"
elif [ $b -gt $a ] && [ $b -gt $c ]; then
echo "$b is the largest"
else
echo "$c is the largest"
fi
}
# 读取用户输入
read -p "input a:" a
read -p "input b:" b
read -p "input c:" c
# 调用函数
find_largest $a $b $c
代码解释:
- **定义函数
find_largest
**:- 使用
local
关键字声明局部变量a
、b
和c
,这样可以避免与全局变量冲突。 - 在函数内部,使用条件判断语句找出三个数中的最大值,并输出结果。
- 使用
- 读取用户输入:
- 使用
read
命令读取用户输入的三个数。
- 使用
- 调用函数:
- 将用户输入的三个数作为参数传递给
find_largest
函数。
- 将用户输入的三个数作为参数传递给
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jungle8884@163.com