以下shell脚本是什么意思,转成Python是什么样的? device_name="device" nvme_slot=slot000 Start_test "${device_name}" >Get_Start_test_log_${nvme_slot}.txt 2>&1 & pid=$! echo "PID:${pid};DEVNAME:${device_name};" >>TestPID.txt
时间: 2024-03-05 10:51:07 浏览: 112
这个shell脚本的意思是先定义了两个变量device_name和nvme_slot,然后调用一个名为Start_test的函数,并将函数的输出重定向到一个名为Get_Start_test_log_${nvme_slot}.txt的文件中,同时将标准错误输出也重定向到同一个文件中,并在后台执行该函数。接着获取该函数的进程ID,并将进程ID和设备名称device_name写入一个名为TestPID.txt的文件中。
将其转换为Python代码可以是这样的:
```python
import os
device_name = "device"
nvme_slot = "slot000"
os.system(f"Start_test {device_name} > Get_Start_test_log_{nvme_slot}.txt 2>&1 &")
pid = os.getpid()
with open("TestPID.txt", "a") as f:
f.write(f"PID:{pid};DEVNAME:{device_name};\n")
```
注意,在Python中没有类似于shell中的函数重定向和后台执行的直接方式,因此需要使用os.system()函数来执行命令,并且将进程ID写入文件的方式也略微不同。
相关问题
以下shell脚本是什么意思,转成Python。 while read slot_current do nvme_sn=$(echo "$slot_current" | awk -F "SN:" '{print $2}') { nvme admin-passthru >nvme.txt 2>&1 RET=$? echo "$RET" > ret.txt exit $RET }& pid=$! echo "$nvme_sn $pid" >>TestPID.txt done < $test_sn_file
这段 shell 脚本的意思是从 `$test_sn_file` 文件中读取每一行字符串,然后提取出其中的 SN,执行 `nvme admin-passthru` 命令并将输出重定向到 `nvme.txt` 文件中,同时记录命令的返回值到 `ret.txt` 文件中,最后将 SN 和该命令的进程 ID 记录到 `TestPID.txt` 文件中。
下面是转换成 Python 的代码:
```python
import subprocess
with open('TestPID.txt', 'w') as pid_file:
with open('ret.txt', 'w') as ret_file:
with open('nvme.txt', 'w') as nvme_file:
with open('test_sn_file', 'r') as sn_file:
for line in sn_file:
nvme_sn = line.split('SN:')[1].strip()
cmd = 'nvme admin-passthru'
process = subprocess.Popen(cmd, shell=True, stdout=nvme_file, stderr=subprocess.STDOUT)
pid = process.pid
ret = process.wait()
ret_file.write(str(ret))
pid_file.write(f'{nvme_sn} {pid}\n')
```
注意,Python 中的 `subprocess.Popen` 函数默认不会阻塞进程,所以需要调用 `wait` 函数等待命令执行完毕,并获取命令的返回值。另外,Python 中的 `open` 函数默认打开文件的模式是只读,需要指定 `w` 模式才能写入文件。
以下shell脚本是什么意思,转换成python是什么样的。ComNvmeDevNameInfoGet() { local slot_info="$1" local ret_file="$2" local ctrl_str="$3" local slot_id="" local slot_temp="" local slot_id_get="" local contrl_num="" local test_scene="" local ctrl_id=0 ls /dev/ | grep "nvme[0-9]\{1,\}" | grep -v "nvme[0-9]\{1,\}n" >nvme_info.txt slot_id=$(echo "${slot_info}" | sed 's/^0*//g') slot_id=$[slot_id+0] slot_id=$[slot_id-1] set +x while read contrl_num || [[ ! -z ${contrl_num} ]] do if [ "0" == "${ctrl_id}" ];then find /sys/devices -name "${contrl_num}" > ${contrl_num}_bus_info.txt else rm -f ${contrl_num}_bus_info.txt dos2unix ${contrl_num}_bus_info.txt fi #兼容服务器的PCIE链路 if grep -iw "FT2-MP1" /root/TestPlat/common/test_scene.ini ;then slot_temp=$(cat ${contrl_num}_bus_info.txt | awk -F / '{print $5}' | awk -F "." '{print $2}') else slot_temp=$(cat ${contrl_num}_bus_info.txt | awk -F "/" '{print $7}' | cut -b 9-10) fi slot_id_get=$[0x${slot_temp}+0] if [ "${slot_id_get}" = "${slot_id}" ];then set -x echo "${slot_id}:/dev/${contrl_num}:Ctrl${ctrl_id}" echo "/dev/${contrl_num}" >"${ret_file}" return 0 fi done < nvme_info.txt set -x echo "$(date +"%y-%m-%d %H:%M:%S") can not get nvme controller ctrl${ctrl_id} info,slot${slot_info}." | tee -a error.txt return 1
这个shell脚本的作用是根据输入的slot_info(slot id)来获取对应的NVMe控制器的设备名,并将其输出到指定的文件中。具体流程如下:
1. 将所有/dev/目录下的名字类似于“nvme[0-9]”但不包含“nvme[0-9]n”的文件名存储在nvme_info.txt文件中。
2. 将输入的slot_info转换为十进制,并减去1,得到slot_id。
3. 遍历nvme_info.txt中的每个文件名,获取其对应的控制器编号contrl_num。
4. 根据contrl_num在/sys/devices目录下查找对应的总线信息文件,并将其存储在contrl_num_bus_info.txt文件中。
5. 根据服务器型号的不同,从contrl_num_bus_info.txt中提取出对应的PCIe插槽编号slot_temp。
6. 将slot_temp转换为十进制,得到slot_id_get。
7. 如果slot_id_get与slot_id相等,则将控制器设备名输出到ret_file文件中,并返回0表示成功。
8. 如果遍历完所有的文件名仍然没有找到对应的控制器设备名,则将错误信息输出到error.txt文件中,并返回1表示失败。
将其转换成Python代码大致如下:
```python
import os
import re
import datetime
def ComNvmeDevNameInfoGet(slot_info, ret_file, ctrl_str):
with open("nvme_info.txt", "w") as f:
for file in os.listdir("/dev/"):
if re.match("nvme[0-9]+", file) and not re.match("nvme[0-9]+n", file):
f.write(file + "\n")
slot_id = int(slot_info.lstrip("0"))
slot_id -= 1
ctrl_id = 0
with open("nvme_info.txt", "r") as f:
for contrl_num in f:
contrl_num = contrl_num.strip()
if ctrl_id == 0:
with open(f"/sys/devices/*/pci*/{contrl_num}/", "w") as bus_info_file:
bus_info_file.write(contrl_num)
else:
os.remove(f"{contrl_num}_bus_info.txt")
os.system(f"dos2unix {contrl_num}_bus_info.txt")
if "FT2-MP1" in open("/root/TestPlat/common/test_scene.ini").read():
with open(f"{contrl_num}_bus_info.txt", "r") as bus_info_file:
slot_temp = re.search(r"/pci([0-9a-fA-F]{2}).*/", bus_info_file.read()).group(1)
else:
with open(f"{contrl_num}_bus_info.txt", "r") as bus_info_file:
slot_temp = re.search(r"/pci.*/(.*):.*", bus_info_file.read()).group(1)[8:10]
slot_id_get = int(slot_temp, 16)
if slot_id_get == slot_id:
with open(ret_file, "w") as ret_file:
ret_file.write(f"/dev/{contrl_num}")
return f"{slot_id}:/dev/{contrl_num}:Ctrl{ctrl_id}"
ctrl_id += 1
with open("error.txt", "a") as error_file:
error_file.write(f"{datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')} can not get nvme controller ctrl{ctrl_id} info,slot{slot_info}.\n")
return 1
```
阅读全文