linux-tutorial/codes/shell/文件操作/创建临时文件.sh

19 lines
337 B
Bash
Raw Normal View History

2019-10-29 18:22:19 +08:00
#!/usr/bin/env bash
2019-05-10 11:17:57 +08:00
2020-01-03 22:41:37 +08:00
tempFile=`mktemp test.XXXXXX`
2019-05-10 11:17:57 +08:00
2020-01-03 22:41:37 +08:00
exec 3> ${tempFile}
2019-05-10 11:17:57 +08:00
2020-01-03 22:41:37 +08:00
echo "This script writes to temp file ${tempFile}"
2019-05-10 11:17:57 +08:00
echo "This is the first line" >&3
echo "This is the second line" >&3
echo "This is the last line" >&3
exec 3>&-
echo "Done creating temp file. The contents are:"
2020-01-03 22:41:37 +08:00
cat ${tempFile}
2019-05-10 11:17:57 +08:00
2020-01-03 22:41:37 +08:00
rm -f ${tempFile} 2> /dev/null
2019-05-10 11:17:57 +08:00