uTools-Manuals/docs/linux/tempfile.html
2019-04-21 11:50:48 +08:00

17 lines
1.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="tempfile">tempfile</h1>
<p>shell中给临时文件命名</p>
<h2 id="补充说明">补充说明</h2>
<p>有时候在写Shell脚本的时候需要一些临时存储数据的才做最适合存储临时文件数据的位置就是<code>/tmp</code>,因为该目录中所有的内容在系统重启后就会被清空。下面是两种方法为临时数据生成标准的文件名。</p>
<h3 id="tempfile命令">tempfile命令</h3>
<p><code>tempfile命令</code>只有在基于Debian的发行版中才默认自带比如Ubuntu其他发行版没有这个命令。</p>
<p>用tempfile命令为一个临时文件命名</p>
<pre><code class="language-bash">temp_file_name=$(tempfile)</code></pre>
<p>用一个加带了随机数的文件名作为临时文件命名:</p>
<pre><code class="language-bash">temp_file_name=&quot;/tmp/file_$RANDOM&quot;</code></pre>
<p>$RANDOM是一个返回随机数的环境变量。</p>
<h3 id="变量">$$变量</h3>
<p>如果没有tempfile命令的Linux发行版也可以使用自己的临时文件名</p>
<pre><code class="language-bash">temp_file_name=&quot;/tmp/file.$&quot;</code></pre>
<p><code>$$</code>是系统预定义变量,显示当前所在进程的进程号,用<code>.$$</code>作为添加的后缀会被扩展成当前运行脚本的进程id。</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->