将变量导出到另一个脚本

时间:2014-03-03 03:52:11

标签: linux bash

我正在制作2个剧本。第一个脚本将获取一个文件,然后将其移动到名为“Trash”的目录中。第二个脚本将恢复此文件并将其发送回其原始目录。到目前为止,我有第一个正确移动文件的脚本。 到目前为止,这是我的代码:

对于delete.sh

FILE=$1
DATEDELETED=$(date)
DIRECTORY=$(pwd)
mv $1 Trash
echo $FILE
echo $DATEDELETED
echo $DIRECTORY

输出:

trashfile
Sun Mar 2 21:37:21 CST 2014
/home/user

对于undelete.sh:

PATH=/home/user/Trash
for file in $PATH
do
$file | echo "deleted on" | date -r $file
done
echo "Enter the filename to undelete from the above list:"
编辑:所以我意识到我不需要变量,我可以列出垃圾目录中的所有文件,并将输出编辑为我想要的。虽然我的for语句有点麻烦,但是我收到了这两个错误:./ undelete.sh:第6行:date:命令未找到 ./undelete.sh:line 6:/ home / user / Trash:是一个目录。所以我不能确定我的陈述中我做错了什么。

这是预期的输出:

file1 deleted on Tue Mar 16 17:15:34 CDT 2010
file2 deleted on Tue Mar 16 17:15:47 CDT 2010
Enter the filename to undelete from the above list:

2 个答案:

答案 0 :(得分:0)

使用source

运行脚本
source <yourscript>

. ./<yourscript>

在你的情况下

. ./delete.sh && ./undelete.sh

希望这会有所帮助

答案 1 :(得分:0)

我已经完成了你的方案试图完成的解决方法。

基本上你可以输入echo“script2variable = $ script1variable”&gt;&gt;来自script1.sh的script2.sh。然后使用source命令稍后从您想要的任何脚本调用该脚本。可能只需要参与所涉及的理论。

祝你好运!

删除脚本文件

#!/bin/bash
# delete.sh file
# Usage: ./delete.sh [filename]
#DATEDELETED=$(date) #not best solution for this kind of application
DIR=$(pwd)
fn=$1
#Specify your trash directory or partition
trash=~/trash
#Set path and filename for simple use in the script
trashed=$trash/$fn.tgz
#Send variables to new manifest script.
echo "#!/bin/bash" > $1.mf.sh
echo "DIR=$DIR" >> $1.mf.sh
# Questionable need? 
#echo "DATEDELETED=$DATEDELETED" >> $1.mf.sh
echo "mv $1 $DIR"  >> $1.mf.sh
echo Compressing
tar -cvpzf $trashed $1 $1.mf.sh
if [ $? -ne 0 ]; then
    echo Compression Failed
else
    echo completed trash compression successfully
    echo Trashbin lists the file as $trashed
    rm $1 -f 
    rm $1.mf.sh -f
    echo file removed successfully
fi
exit 0

恢复脚本文件

#!/bin/bash
# restore.sh
# Usage: ./restore.sh 
# filename not required for this script, use index selection
fn=$1
#SET LOCATION OF TRASH DIRECTORY
trash=~/trash
listfile=($(ls $trash))
#SET COUNTER FOR LISTING FILES = 0
i=0
#THIS IS JUST A HEADER FOR YOUR OUTPUT.
clear #cleanup your shell
echo -e INDEX '\t' Filename '\t' '\t' '\t' Date Removed
#Echo a list of files from the array with the counter.
for FILES in "${listfile[@]}"
do
    echo -e $i '\t' $FILES '\t' "deleted on $(date -r $trash/$FILES)"
    let "i += 1"
done
#Output total number of items from the ls directory.
echo -e '\n' $i file\(s\) found in the trash!
# 1 Offset for 1 = 0, 2 = 1, and so on.
let "i -= 1"
#Require input of a single, double, or triple digit number only.
#Loop back prompt if not a number.
while true;
do
    read -p "Enter an index number for file restoration: " indx
    case $indx in
        [0-9]|[0-9][0-9]|[0-9][0-9][0-9] ) break ;;
        * ) read -p "Please enter a valid number 0-$i: " indx ;;
    esac
done
#
script=$(echo ${listfile[$indx]}|sed 's/\.tgz/\.mf.sh/g')
tar -xvpzf $trash/${listfile[$indx]}
rm $trash/${listfile[$indx]}
sleep 2
chmod +x $script
source $script
rm $script