2

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 e.g (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
Eizan
  • 21
  • 2
  • Please be more specific than "some help". What specifically have you tried and what specific problem did you encounter? Please show your attempted code. [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – kaylum Aug 05 '20 at 04:29
  • @kaylum thanky you for your feedback it has been updated with what i have tried so far. – Eizan Aug 05 '20 at 04:38
  • And what problem(s) do you have with what you tried? Please review [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – kaylum Aug 05 '20 at 04:40

1 Answers1

0

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"
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Thank you for your response, I have tried the solution you have provided but still getting an error bad filename. from what i can see when i tested it in a manual manner is that, in the part where `tftp://ipaddress/"$filename"` the filename variable is put as the directory path of the file and not just the filename. is there a way that it puts just the filename for that section. – Eizan Aug 05 '20 at 20:26
  • @Eizan : To get just the name of the file without the path, you get this using `basename` by `bare_filename=$(basename "$filename")`. – user1934428 Aug 06 '20 at 06:39