指定されたパスがファイルかディレクトリであるかを確認する方法

指定されたパスがファイルかディレクトリであるかを確認する方法

スクリプト(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

コマンドはうまくいきます。並列に実行される3つのスクリプト(3つのスクリプトが同じ)でこのコマンドを使用すると問題が発生します。エラーが発生します。

error writing "stdout": bad file number

これら3つのスクリプトは同時に同じファイルを呼び出すためです。それでは、誰でもこの問題を解決するのに役立ちますか?

これは私のスクリプトです:----

#!/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]

おすすめ記事