2

I have this installer shell script in a bash script.

  1. I am doing a rename of the existing text file (backing it up).
  2. I remove old file
  3. Copy fresh file to the target dir

    mv /target/data.ini /target/data_$(date +"%Y%m%d_%H%M%S").ini       
    rm -f /target/data.ini     
    cp /install/data.ini /target/data.ini
    

For some reason the cp command does not always copy the file.
Is it possible that previous mv or rm operation was not completed?

I am unable to see any error since its running as part of the script; if I execute the commands manually it's working fine.

11
  • 2
    What does "not always working" mean? Does the file not copy? Does it give an error? Does it end up as the date-stamped version?
    – Jeff Schaller
    Aug 26, 2019 at 18:26
  • 1
    Is /target an automounted filesystem?
    – Jeff Schaller
    Aug 26, 2019 at 18:33
  • 4
    Scripts still output errors. E.g., by default scripts run from cron will email their errors. The logger command is very useful to send script output (including errors) to the system log or journal. Capturing the error messages would make it much easier to debug the issue. I would suggest getting the error message logged or emailed as much easier than taking random guesses at what might be going wrong.
    – derobert
    Aug 26, 2019 at 18:38
  • 2
    What are the error messages? Aug 26, 2019 at 18:44
  • 2
    Try adding sleep 2 ; before cp command. It may slow down but can increase precision.
    – Baraujo85
    Aug 27, 2019 at 15:17

1 Answer 1

1

If I execute the commands manually it's working fine.

There is a clue. It could be a path problem. When I write something, especially for a script, I always like to include the path to the command.

$ which date
/usr/bin/date

Then, I would add error checking in the script:

if [ -f /target/data.ini ]
then
  # Note spaces separating the parenthesis from the command
  /bin/mv /target/data.ini /target/data_$( /usr/bin/date +"%Y%m%d_%H%M%S" ).ini
  if [ $? -ne 0 ]
  then
     echo "Error on MV command"
     exit
  fi
  else
    echo "Error: Can't find /target/data.ini"
    exit
  fi
  cp /install/data.ini /target/data.ini  
  #Same type of error checking here 

That ought to fix or shed some light on your error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .