1

I am using the command below in my expect script (in tcl/Tk) for checking whether the entered path is an individual file or directory:

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

The above command invokes check.sh file. Its contents are:

#!/bin/bash

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

The command runs fine. The problem occurs when I am using the command in three parallel running scripts(the three scripts are same) it gives me an error

error writing "stdout": bad file number

because the three scripts are invoking the same file at the same time. So, can anyone help me with this?

Here's my script:----

#!/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"
}
7
  • 2
    Why don't you use [file isdirectory $file1] to do that? – user218374 Jun 20 '17 at 7:20
  • @RakeshSharma Could you also suggest how to fetch epoch time(in milliseconds) in expect script – Bhavya Jain Jun 20 '17 at 7:31
  • set X [clock milliseconds] or this: set X [expr {[clock seconds] * 1000}] – user218374 Jun 20 '17 at 7:45
  • @BhavyaJain with "helped a lot", do you mean it answered your question? – Volker Siegel Jun 20 '17 at 7:53
  • I got my answer but I am still getting the same error – Bhavya Jain Jun 20 '17 at 8:22
1

You apparently have your answer in the comments. I'd write your code like this, which removes the need to call out to your time and check.sh scripts. It also reduces the code duplication.

#!/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]
1
  • In your script,the scp command is not copying all the files in the directory. – Bhavya Jain Jun 21 '17 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.