linux - 将变量导出到另一个脚本

标签 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: line 6: date: command not find ./undelete.sh:第6行:/home/user/Trash:是一个目录。所以我不太确定我的 for 语句中做错了什么。

这是预期的输出:

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:

最佳答案

好吧,我已经完成了您的方案想要实现的目标的解决方法。

基本上你可以从 script1.sh 输入 echo "script2variable=$script1variable">> 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

关于linux - 将变量导出到另一个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138369/

相关文章:

bash - 匹配两个文件第一列中的值并将匹配行加入新文件中

bash - 在 bash 脚本中存储日期变量

linux - 将十进制值作为两个小端十六进制字节输出到文件

c - exec* 函数中的前两个参数是否包含冗余信息?

linux - 是否可以创建一个始终以 root 身份启动程序的 shell 脚本?不管是谁在运行它

Linux/Unix Socket自连接

linux - 如何构建体积更小的 glibc?

json - 如何将 jq 与变量连接?

python - 如何制作可执行 shell 脚本来运行我的 .py 程序并输入文件

linux - 如何通过/etc/fstab 为 Centos 7 设置静态挂载