>

따라서 사용자가 bash copy.sh copysource.txt copydest.txt를 입력하고 파일이 유효한 경우 copysource를 copydest에 추가 한 다음 cat을 통해 파일을 표시하려고합니다

-파일 이름이 정확하지 않거나 찾을 수없는 경우 ... 아래 코드를 작성하라는 메시지가 표시됩니다

#!/bin/bash
#Date: July 14 2016
if [ "$1" == "copysource.txt" ] && [ "$2" == "copydest.txt ]; then
cp $1 $2
echo "copies $1 to $2"
echo "contents of $2 are: "
cat $2
fi
#if the above was correct I want to script to stop
#otherwise if the info above was incorrect re: file names I want to user to be
#prompted to below code

clear
echo -n "Enter the source file name: "
read sourc

if [ "$sourc" == "copysource.txt" ]; then
    echo "The file name you entered was found!";
else
    echo "The file name you entered was invalid - please try again";
    echo -n "Enter the source file name: "
    read sourc
fi
echo -n "Enter the destination file name: "
read dest

if [ "$dest" == "copydest.txt" ]; then
    echo "The destination file name you entered was found!";
else
    echo "The destination file name you entered was invalid - please try         again";
    echo -n "Enter the destination file name: "
    read dest
fi

cp $sourc $dest
echo "copies $sourc to $dest"
echo "contents of $dest are: "
cat $dest


  • 답변 # 1

    다음은 두 파일 ($1 및 $2로 전달됨)이 존재하고 $1에서 $2로 연결되어 있는지 확인합니다 (그것이 당신이하려는 것이라고 생각합니다). 파일 중 하나가 존재하지 않으면 기존 파일을 입력 할 때까지 sourc 및 dest 파일 이름을 차례로 입력하라는 메시지가 표시됩니다 (그렇지 않으면 종료하려면 ctrl-c)!

    #!/bin/bash
    #Date: July 14 2016
    # Use -f to test if files exist
    if [[ -f "$1" ]] && [[ -f "$2" ]]; then
        echo "Appending contents of $1 to $2"
        # Use cat to take the contents of one file and append it to another
        cat "$1" >> "$2"
        echo "Contents of $2 are: "
        cat "$2"
        # Don't want to go any further so exit.
        exit
    fi
    
    # You could consider merging the logic above with that below
    # so that you don't re-test both files if one of them exists, but the
    # following will work.
    clear
    sourc=''
    dest=''
    while [[ ! -f "$sourc" ]]
    do  
        echo -n "Enter the source file name: "
        read sourc
        if [ -f "$sourc" ]; then
            echo "The file name you entered was found!";
        else
            echo "The file name you entered was invalid - please try again";
        fi
    done
    while [[ ! -f "$dest" ]]
    do
        echo -n "Enter the destination file name: "
        read dest
        if [ -f "$dest" ]; then
            echo "The file name you entered was found!";
        else
            echo "The file name you entered was invalid - please try again";
        fi
    done
    cat "$sourc" >> "$dest"
    echo "Appending $sourc to $dest"
    echo "Contents of $dest are: "
    cat "$dest"
    
    

  • 답변 # 2

    트릭 함수를 사용하여 트릭을 수행해야합니다

    appendFile(){
      local iFile oFile;
      [ ! -f "$1" ] || [ -z "$2" ] && {
        while [ ! -f "$iFile" ]; do read -p "Input file: " -i "$1" iFile; done;
        read -i "$2" -p "Output file: " oFile;
      };
      cat "${iFile-$1}" >> "${oFile-$2}";
      cat "${oFile-$2}";
    }
    
    

관련 자료

  • 이전 mkfs - --force 플래그를 사용하여 마운트 된 USB 드라이브를 포맷하면 어떻게됩니까?
  • 다음 sshd - SSH를 통해 로그인 한 사용자가 Active Direcory를 통과했는지 확인