shell读取文件的内容作为命令执行

发布时间:2021-03-05 23:16:18 阅读:1470次

我经常会将常用的命令写在一个记事本中

这样想执行时,可以方便的操作

比如

cat shell.txt

pwd
date
ls

这个时候该如何执行shell.txt的内容呢

比如想执行date

root@orangepipc:~# eval `cat 1.txt|sed -n '2p'`
或
root@orangepipc:~#  eval $(cat 1.txt|sed -n '2p')

想执行ls

root@orangepipc:~# eval `cat 1.txt|tail -1` 
或
root@orangepipc:~# eval $(cat 1.txt|tail -1)

我们还可以逐行读取并执行

!/bin/bash

while read -r line
do
echo $line
eval $line
done < shell.txt

或者

#!/bin/bash
for  i  in  `cat 1.txt`
do
eval $i
done

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:shell读取文件的内容作为命令执行 出自老鄢博客 | 欢迎分享