写一个读取sn并打印的shell脚本
时间: 2023-10-18 22:03:25 浏览: 32
可以使用以下脚本来读取SN并打印:
```bash
#!/bin/bash
# 读取SN
sn=$(cat /proc/cpuinfo | grep Serial | awk '{print $3}')
# 打印SN
echo "Device SN: $sn"
```
这个脚本使用 `cat` 命令读取 `/proc/cpuinfo` 文件中的信息,然后使用 `grep` 命令过滤出含有 `Serial` 字符串的行,最后使用 `awk` 命令提取出第三个单词即为 SN。然后使用 `echo` 命令打印出来。
相关问题
写一个批量获取SN并对比的shell脚本
以下是一个简单的Shell脚本,可以批量获取SN并对比:
```shell
#定义SN获取函数
get_sn(){
sn=$(dmidecode -t system | grep "Serial Number" | awk '{print $3}')
echo $sn
}
#定义对比函数
compare_sn(){
if [ "$1" == "$2" ]; then
echo "SN一致"
else
echo "SN不一致"
fi
}
#读取SN列表文件
sn_file="sn_list.txt"
#逐行读取SN列表,获取本机SN并进行对比
while read line; do
local_sn=$(get_sn)
compare_sn $local_sn $line
done < $sn_file
```
使用方法:
1. 将需要对比的SN列表保存到一个文本文件 `sn_list.txt` 中,每行一个SN。
2. 将上述脚本保存为 `compare_sn.sh`。
3. 在终端中执行 `chmod +x compare_sn.sh` 赋予执行权限。
4. 执行 `./compare_sn.sh` 开始批量获取SN并对比。
写一个shell脚本读取txt文件
好的,以下是一个读取txt文件的shell脚本:
```shell
#!/bin/bash
# txt文件路径
txt_path="/path/to/txt/file.txt"
# 逐行读取txt文件
while read line; do
echo "$line"
done < "$txt_path"
```
这个脚本会打印出txt文件中每行的内容。你可以将`echo "$line"`改为任何你想要的处理操作。