1

In ".last-time" script, it contains --- 2019-12-30 18:23:05.266000000 +08:00

".last-time" script is in the path /file/.last-time

I want to save the text in ".last-time" script to a variable in "final.sh" script. In "final.sh", I tried

LASTTIME = $(/file/.last-time) 
echo $LASTTIME

but it is not working.

May I know how I can do it. Thanks in advance.

1 Answers1

2

Try

LASTTIME=$(cat /file/.last-time) 
echo $LASTTIME

cat is a multipurpose tool that is used here to copy the contents of the file into the output stream.

UPDATE You can use the date utility to manipulate strings containing date and time variables. For instance the following command prints the date using the default format correctly converting to the timezone set up in the current shell context.

date -d "${LASTTIME##--- }"
Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76
  • is there a way to keep LASTTIME as a timestamp variable instead of string variable? – KeenLearner Jan 02 '20 at 09:43
  • @KeenLearner BASH keeps all its variables as character strings. However you can use the `date` tool to manipulate dates. – Dima Chubarov Jan 02 '20 at 10:11
  • Thanks for the prompt reply. By using this date -d "${LASTTIME##--- }" , date is converted to Mon Dec 30 18:23:05 +08 2019. Is there anyway to keep it in timestamp format(exact format in .last-time file) and not date format? – KeenLearner Jan 03 '20 at 02:55
  • 1
    @KeenLearner I am not sure I fully understand your requirements. Using the custom format option to date you could get pretty close to the original format: `date -d "${LASTTIME##--- }" +"%Y-%m-%d %H:%M:%S.%N %:z"`. Also `date` gives you options to perform add or subtract time intervals to or from your date. – Dima Chubarov Jan 03 '20 at 05:19