0
votes

I have a script with the name bckp2 and a text file with the name variables.txt. When I am running the script like that in the terminal ./bckp2 it works exactly as I want, but when I try to run it with the at like this at -f bckp2 19:30 it doesn't work, it doesn't do the same thing. With the at command it seems like it doesn't read from the file and I have no idea why.

script bckp2 Reads 3 words from the text file variables.txt and is using them as source and destination for tar command. Just ignore the first word it's for testing

let count=0

while read line; do
 for word in $line; do
  if [ $count -eq 0 ]
   then
     username=$word
  elif [ $count -eq 1 ]
   then
     source=$word
  elif [ $count -eq 2 ]
   then
     destination=$word
  fi
  let count=count+1
 done
done < variables.txt

echo $destination $source  > test.txt
tar -cvf $destination.tar $source

Text file variables.txt

valkon fake faketar

In the script bckp2 I am saving the file content into variables, only to see if the at works or not, and it doesn't. It's writing to file blank text, so I assume that the script doesn't read from file at all. But as I told you, when I am running it like ./bckp2 it works.

1
Your whole while read ... < variables.txt would be better written as read username source destination < variables.txt... Keep in mind that jobs run via at or batch may not have entirely the same environment as your terminal session (although it does try to preserve much of it), and they are often run by /bin/sh instead of /bin/bash - so you may need a #!/bin/bash at the top of your script...twalberg
Yes this it, everything you said solved my problem, the while loop and the #!/bin/bash . Can you answer the question so I will be able to accept your answer?Manolis Pap

1 Answers

1
votes

at and batch default to interpreting commands with /bin/sh, not /bin/bash. If you want to use a different shell, you need to format your script appropriately, by adding #!/bin/bash as the first line of the script.

Also, your unnecessarily complicated loop for reading variables could be simply replaced with

read username source destination < variables.txt