我正在将一些软件从 Unix 迁移到 Linux。
我有以下脚本;它是文件传输的触发器。
命令有什么exec
作用?
它们也能在 Linux 上工作吗?
#!/bin/bash
flog=/mypath/log/mylog_$8.log
pid=$$
flog_otherlog=/mypath/log/started_script_${8}_${pid}.log
exec 6>&1
exec 7>&2
exec >> $flog
exec 2>&1
exec 1>&6
exec 2>&7
/usr/local/bin/sudo su - auser -c "/mypath/bin/started_script.sh $1 $pid $flog_otherlog $8"
启动的脚本如下:
#!/bin/bash
flusso=$1
pidpadre=$2
flogcurr=$3
corrid=$4
pid=$$
exec >> $flogcurr
exec 2>&1
if [ $1 = pippo ] || [ $1 = pluto ] || [ $1 = paperino ]
then
fullfile=${myetlittin}/$flusso
filename="${flusso%.*}"
datafile=$(ls -le $fullfile | awk '{print $6, " ", $7, " ", $9, " ", $8 }')
dimfile=$(ls -le $fullfile | awk '{print $5 " " }')
aaaammgg=$(ls -E $fullfile | awk '{print $6}'| sed 's#-##g')
aaaamm=$(echo $aaaammgg | cut -c1-6)
dest_dir=${myetlwarehouse}/mypath/${aaaamm}
dest_name=${dest_dir}/${filename}_${aaaammgg}.CSV
mkdir -p $dest_dir
cp $fullfile $dest_name
rc_copia=$?
fi
我将ls -le
在Linux 中ls -l --time-style="+%b %d %T %Y"
进行ls -E
更改。ls -l --time-style=full-isoand
exec [n]<&word
将在 bash 中复制输入文件描述符。exec [n]>&word
将在 bash 中复制一个输出文件描述符。见 3.6.8:https ://www.gnu.org/software/bash/manual/html_node/Redirections.html
但是,参数的顺序可能会令人困惑。
在您的脚本中:
exec 6>&1
创建文件描述符的副本1
,即 STDOUT,并将其存储为文件描述符6
。exec 1>&6
复制6
回1
.它也可以通过附加破折号来移动,即
1<&6-
关闭描述符6
并只留下1
.在这两者之间,您通常会发现写入 STDOUT 和 STDIN 的操作,例如在子外壳中。
另请参阅:移动文件描述符的实际用途
exec {number x}>&{number y}
将文件描述符 X 复制到 Y 中。文件描述符用法:
在您的情况下,它们应该在之前的某个地方打开,例如
exec 3<> /tmp/some_file
将 fd3 设置为某个文件。通常,您可以执行 exec
2>&1
以将 stderr 输出重定向到 stdout。您的 bash 示例不完整,因为
$8
引用了给您的脚本的八个参数,因此我们在这里肯定缺少一些东西,例如参数 2-7 ...