我有一个小脚本可以将所有文件从一个目录(SRC)复制到另一个目录(DES)。以下脚本运行正常。
#!/bin/bash
SRC="/home/user/dir1/*"
DES="/home/user/dir2/"
for file in "$SRC"
do
if [ -f "$file" ]
then
cp "$file" "$DES"
echo "$file -----> file copied"
fi
done
现在我在考虑将文件从一个目录复制到另一个目录时,如果该文件已经存在于(DES)目录中,并且具有相同名称的(SRC)目录,则如何跳过复制文件,并像往常一样继续保留其余文件从源头到目的地?
这里我如何使用break并继续循环来执行此操作?
谢谢,
答案 0 :(得分:3)
我建议使用<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array( 'registration_ids' => $tokens, 'data' => $message);
$headers = array('Authorization:key =' . GOOGLE_API_KEY,'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
include_once 'config.php';
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$id = $_GET['id'];
$sql = "Select Token From member where M_Id='" . $id . "'";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$myMessage = $_GET['message']; //폼에서 입력한 메세지를 받음
if ($myMessage == ""){
$myMessage = "Nothinh here";
}
$message = array("message" => $myMessage);
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
:
rsync
交换机src="/home/user/dir1/"
dst="/home/user/dir2/"
rsync -rav --ignore-existing "${src}" "${dst}"
告诉--ignore-existing
跳过目的地的文件。
答案 1 :(得分:2)
为什么不将整个脚本缩减为oneliner?
cp -n /home/user/dir1/* /home/user/dir2/
-n
标记(--no-clobber
)阻止cp
覆盖现有文件。
如果您的实际情况更复杂,您还可以查看rsync
。