주어진 경로가 파일인지 디렉토리인지 확인하는 방법

주어진 경로가 파일인지 디렉토리인지 확인하는 방법

스크립트(tcl/Tk)에서 다음 명령을 사용하여 expect입력된 경로가 단일 파일 또는 디렉터리인지 확인합니다.

set b [exec ./check.sh $file1 | awk -F {=} {{print $1}} ]

위의 명령은 check.sh파일을 호출합니다. 그 내용은 다음과 같습니다:

#!/bin/bash

if [[ -f "$1" ]] 
then
    echo "File"
else
    if [[ -d "$1" ]] 
    then
        echo "Directory"
    else 
        echo "Other"
    fi
fi

명령이 잘 작동합니다. 병렬로 실행되는 세 개의 스크립트(세 개의 스크립트가 동일함)에서 이 명령을 사용할 때 문제가 발생합니다. 오류가 발생합니다.

error writing "stdout": bad file number

이 세 스크립트는 동시에 동일한 파일을 호출하기 때문입니다. 그렇다면 누구든지 이 문제를 해결하도록 도와줄 수 있나요?

이것은 내 스크립트입니다:----

#!/usr/bin/expect

#Set the timeout time for expect command
set timeout 5

#First Argument is Ip Address
set ip [lindex $argv 0]

#Seond Argument is UserName
set user [lindex $argv 1]

#Third Argument is Password
set password [lindex $argv 2]

#Fourth Argument is Path to the source File to be copied (Keeping in mind that this path is concatenated with the second path in the program)
set file1 [lindex $argv 3]

#Fifth Argument is the destination path
set file2 [lindex $argv 4]

#Fetches the epoch time by executing "time" script
set t1 [exec ./time | awk -F {=} {{print $1}} ]

#Checks whether the path to copied is an individual file or a directory
set b [exec ./check.sh $file1 | awk -F {=} {{print $1}} ]

if { $b == "File" } {
    puts "It is a file"

    #Executes the scp command
    spawn bash -c "scp -p $file1 $user@$ip:$file2"

    #Sends the password
    expect "password:"
    send "$password\r";
    interact

    puts "Number of Files Copied: 1"
}

if { $b == "Directory" } {
    puts "It is a directory";

    #Executes the scp command
    spawn bash -c "scp -r -p $file1 $user@$ip:$file2"

    #Sends the password
    expect "password:"
    send "$password\r";
    interact

    #For calculating the number of files copied
    set c [exec find $file1 -type f | wc -l | awk -F {=} {{print $1}}]

    puts "Number of Files Copied: $c\n"
}

답변1

분명히 댓글에 답이 이미 나와 있습니다. 귀하의 코드를 이와 같이 작성하므로 귀하 time와 스크립트를 호출할 필요가 없습니다 check.sh. 또한 코드 중복도 줄어듭니다.

#!/usr/bin/expect

# assign command line arguments to variables
#  First Argument is Ip Address
#  Seond Argument is UserName
#  Third Argument is Password
#  Fourth Argument is Path to the source File to be copied (Keeping in mind that this path is concatenated with the second path in the program)
#  Fifth Argument is the destination path

lassign $argv ip user password file1 file2

set start [clock milliseconds]

set scp_args {-p}

if {[file isdirectory $file1]} {
    puts "It is a directory"
    lappend scp_args "-r"
    set c [exec find $file1 -type f | wc -l]

} elseif {[file isfile $file1]} {
    puts "It is a file"
    set c 1

} else {
    puts "$file1 is a [file type $file1]"
    exit 1
}

puts "Number of Files to be copied: $c"

#Set the timeout time for scp command
set timeout 5

#Executes the scp command
spawn scp {*}$scp_args $file1 $user@$ip:$file2

expect "password:"
send "$password\r"

expect eof
close

set end [clock milliseconds]
set elapsed [expr {($end - $start) / 1000.0}]
puts [format "duration: %.3f seconds" $elapsed]

관련 정보