简体   繁体   中英

CENTOS - Bash script to initiate a file transfer from a directory

I am trying to create a bash script to initiate a file transfer to another machine via tftp app. currently i would do this manually by running the command ./tftp "filename" tftp://ipaddress/filename.

What i would like to do is have a bash script that looks at a folder eg (filetransfer) for any files an initiates that same command. can someone please help? as i am a noob at bash scripting

so far i have tried the below

when running this is says that the filename is bad

#!/bin/bash
for filename in ./*
do
  ./tftp "$filename" tftp://ipaddress/"$filename"
done

also tried this

when running this one below it transfers everything on the directory below it.

#!/bin/bash
cd /path/to/the/directory/*
for i in *
do
  ./tftp "$i" tftp://ipaddress/"$i"
done

In the code you posted, filename , respecitvely i , can also take the name of a subdirectory, since you are looping over all entries in the directory. If you want to restrict the transfer to plain files, do a

[[ -f $filename ]] && ./tftp "$filename" tftp://ipaddress/"$filename"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM