Discussion:
How to update environment variable output
(too old to reply)
Keith Bainbridge
2024-07-23 07:10:01 UTC
Permalink
Good afternoon All

For reference, today is Tue ***@15:41:47 205.2024 AEST

This is part of my command prompt, generated by

PS1='\n \u@\h \n\n $(date +"%a %d%b%Y@%H:%M:%S %j.%Y %Z") \n :\w $> '

My calculation is that today is day 205

When I run this function


mkcd ()
{
mkdir -p $1
cd $1
}
in the form :~ $> mkcd
/mnt/data/keith/Documents/$YEAR/$MTH$YEAR/$DOYR.$YEAR/

I am transferred to
/mnt/data/keith/Documents/2024/Jul2024/196.2024 $> ie $DOYR is
using the wrong day number

So I cd to my home and run the commands separately, in the form

:~ $> mkdir -p /mnt/data/keith/Documents/$YEAR/$MTH$YEAR/$DOYR.$YEAR/

***@lenv0

Tue ***@15:38:24 205.2024 AEST
:~ $> cd /mnt/data/keith/Documents/$YEAR/$MTH$YEAR/$DOYR.$YEAR/

and land in /mnt/data/keith/Documents/2024/Jul2024/196.2024

ls -lah /mnt/data/keith/Documents/2024/Jul2024
total 104K
drwxr-xr-x 8 keith keith 4.0K Jul 22 13:33 .
drwxr-xr-x 30 keith keith 4.0K Jul 13 11:53 ..
sdrwxr-xr-x 2 keith keith 4.0K Jul 22 13:33 196.2024
drwxr-xr-x 2 keith keith 4.0K Jul 13 11:53 day189.2024
drwxr-xr-x 2 keith keith 4.0K Jul 13 11:53 day192.2024
drwxr-xr-x 2 keith keith 4.0K Jul 13 11:53 day193.2024
drwxr-xr-x 2 keith keith 4.0K Jul 13 11:53 day194.2024
drwxr-xr-x 2 keith keith 4.0K Jul 22 13:21 day196.2024

shows that directory 196.2024 was created yesterday. (My original
format for the new dir was $MTH$YEAR/day$DOYR.$YEAR/ I took the 'day'
out in case that was creating a problem that I haven't seen before.


OK then, I have 2 questions:

Why is the env var $DOYR not updating when I use it at the command
prompt AND for that matter in a script - clearly the system is or I'd be
getting the wrong day # at my command prompt?

Why am I not getting a warning that the dir already exists?

I want to run the script as a cron job, to keep a daily copy of a few
files - mainly to prove that I can do it.

Thanks for any suggestions

By the bye, I expanded the date format for my command prompt as part of
the discussion I started a few weeks back about the date the sender sent
mail showing in a reply.
--
All the best

Keith Bainbridge

***@gmail.com
***@gmail.com
+61 (0)447 667 468

UTC + 10:00
Greg Wooledge
2024-07-23 11:20:01 UTC
Permalink
Post by Keith Bainbridge
mkcd ()
{
mkdir -p $1
cd $1
}
You're missing quotes. Two sets. You probably also want && between
the two commands, to check for the success of the mkdir before attempting
a cd.
Post by Keith Bainbridge
in the form :~ $> mkcd
/mnt/data/keith/Documents/$YEAR/$MTH$YEAR/$DOYR.$YEAR/
Where do these variables (YEAR, MTH, DOYR) come from?
Post by Keith Bainbridge
I am transferred to
/mnt/data/keith/Documents/2024/Jul2024/196.2024 $> ie $DOYR is using
the wrong day number
How/where did you set it?
Post by Keith Bainbridge
Why is the env var $DOYR not updating when I use it at the command prompt
AND for that matter in a script - clearly the system is or I'd be getting
the wrong day # at my command prompt?
We don't know, because you haven't shown us where you're setting it.
Post by Keith Bainbridge
Why am I not getting a warning that the dir already exists?
Because you used mkdir -p. Please read what -p does.
Post by Keith Bainbridge
I want to run the script as a cron job, to keep a daily copy of a few files
- mainly to prove that I can do it.
A cron job will not inherit those variables from your shell session,
or wherever it is you're defining them. You'll have to set them within
the cron job, or use the date(1) command, or use bash's printf %()T
formatter to generate date/time strings.

Loading...