我的bash脚本有问题

时间:2017-12-16 18:42:13

标签: linux bash scripting

我正在编写一个脚本,允许用户通过允许他们输入文件名来创建他们选择的文件的备份。然后,该文件将进行备份,然后在其末尾标记日期并保存在主驱动器上。但每当我尝试运行它时,我都会收到一个错误:cp:在' _backup_2017_12_16'

之后缺少目标文件操作数

这是我的代码:

function sendMediaGroup($files)
{
    $url = "https://api.telegram.org/bot" . $this->token . "/" . __FUNCTION__;
    $media = [];
    $ch = curl_init();
    $type = "photo";
    $caption = "";

    foreach ($files as $file)
    {
        $media[] = [
            'type' => $type,
            'media' => $file['tmp_name'],
            'caption' => $caption
        ];
    }

    $disable_notification = false;
    $reply_to_message_id = null;
    $parameters = [
        'chat_id' => $this->chat_id,
        'media' => json_encode($media),
        'disable_notification' => $disable_notification,
        'reply_to_message_id' => $reply_to_message_id,
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);

    return $output = curl_exec($ch);
}

1 个答案:

答案 0 :(得分:2)

  1. 您的case声明目前为空。您需要它来处理您选择的选项
  2. 参数之间需要有空格:cp source dest
  3. 如果您使用数组作为选项,您也可以将Quit放在那里
  4. 如果选择创建备份的选项,则需要提示用户输入文件名。 read命令用于获取用户输入
  5. 总而言之,您的脚本可能如下所示:

    #!/usr/bin/env bash
    
    options=("Backup" "Quit")
    prompt="Enter: "        
    title="My script 3" 
    
    echo "$title"
    PS3=$prompt
    
    select opt in "${options[@]}"; do
       case $opt in
          "Backup")
              IFS= read -r -p "Enter filename: " filename 
              cp -- "$filename" "${filename}_backup_$(date +%Y_%m_%d)" && echo "Backup created..."
              ;;
            "Quit") break ;;
                 *) echo "Wrong option..." ;;
       esac
    done
    
相关问题