trap 的初步应用
#!/bin/bash
trap 'rm -f fred1' INT
touch fred1
echo "Print Ctrl + C "
while [ -f fred1 ]; do
echo "File exist!"
sleep 1
done
echo "the file no longer exists"
trap INT
echo create file
echo > fred1
while [ -f fred1 ]; do
echo "File exists too!"
sleep 1
done
echo we never get here
exit 0
unset 的初步应用
#!/bin/bash
foo="Hello World!"
echo $foo
unset foo
echo $foo
exit 0
本文介绍Bash脚本中trap与unset命令的基本应用实例。通过两个简单的脚本示例,展示了如何使用trap捕获信号并执行清理操作,以及如何使用unset取消变量设置。这些技巧对于编写健壮的Bash脚本非常有用。
707

被折叠的 条评论
为什么被折叠?



