涉及使用cp的rsync和symlink的shell脚本问题

问题描述 投票:0回答:1

正在考虑的脚本已编码如下所示:

#!/bin/bash

#recursive remove

REC_RM="rm -rf"

#recursive copy

REC_CP="cp -Rf"

#rsync

RSYNC_CMD="rsync -av --progress"

#recursive link

REC_LN="cp -as"

#the script excepts three arguments as follows:
#
# a filename such as files.txt with entries along the lines of
#
# [HEADER]
# <PATH>
#  
# the name of the specific HEADER to look for in the file, such as "FILES"
#
# a destination path such as /dst
#
# so the script should be invoked as <script> <filename> <header string> <destination path>
#
# for the sake of brevity, the segment related to argument checking has been omitted

#get the path listed under the specific header from within filename

SRC=$(grep -A1 $2 $1)
   
SRC=$(echo ${SRC} | awk '{print $2}')

#remove any preceding '/' from the path

FWD_SLSH='/'

SRC_MOD=$(echo ${SRC} | sed "s|^$FWD_SLSH\(.*\)|\1|")

#append the modified source path to the destination path

DST=${3}${SRC_MOD}

#create the destination directory path

mkdir -p ${DST}

#sync the source and destination paths

eval "$RSYNC_CMD" ${SRC} ${DST}

#recursively purge source path

eval "$REC_RM" ${SRC}

#recursively link destination path back to source path by means of copy

eval "$REC_LN" ${DST} ${SRC}

文件

/src_vinod/test/data.txt
和目录
/dst_vinod
是在脚本之外创建的。

现在假设文件名

files.txt
包含以下条目:

[FILES]
/src_vinod/test/

脚本将被调用如下所示(名称

script.sh
已被使用):

script.sh filenames.txt FILES /dst_vinod/

执行后,我希望目标路径填充如下:

/dst_vinod/src_vinod/test/data.txt

以及要填充为

的源路径
/src_vinod/test/data.txt

其中

data.txt
是目标路径下文件
data.txt
的软链接

然而,我得到的结果如下:

目标路径包括

/dst_vinod/src_vinod/test/test/data.txt

源路径由

组成
/src_vinod/test/test/data.txt

其中

data.txt
是指向目标路径下的文件data.txt的软链接

我不确定为什么目录

test/
被复制了。

有什么想法吗?

TIA

维诺德

bash symlink rsync cp
1个回答
2
投票

函数是您应该使用的函数,而不是 eval 和变量。

Variables hold data. Functions hold code.


类似的东西:

#!/usr/bin/env bash

rec_rm(){ rm -rf "$@"; }

rec_cp(){ cp -Rf "$@" ;}

rsync_cmd(){ rsync -av --progress "$@"; }

rec_ln(){ cp -as "$@"; }

src=$(grep -A1 "$2" "$1" | tail -n+2) || exit
src=${src/\/\//\/}

dst=$3$src
dst=${dst/\/\//\/}

mkdir -vp "$dst" || exit

rsync_cmd "$src" "$dst" || exit

rec_rm "$src" || exit

rec_ln "$dst" "$src" || exit 

创建目录和虚拟/空文件后,使用参数执行脚本,输出如下:

mkdir: created directory '/dst_vinod/src_vinod'
mkdir: created directory '/dst_vinod/src_vinod/test/'
sending incremental file list
./
data.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 109 bytes  received 38 bytes  294.00 bytes/sec
total size is 0  speedup is 0.00

检查文件/目录:

file /src_vinod/test/data.txt

输出

/src_vinod/test/data.txt: symbolic link to /dst_vinod/src_vinod/test/data.txt

© www.soinside.com 2019 - 2024. All rights reserved.