Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First of all, to pass the value of my touch-able date, I have this alias:
Code:
alias superdate='export tdate'
tdate is the variable to which the date string is assigned from terminal, usually something like 201601120824.46
The script looks like this:
Code:
#!/bin/bash -i
SAVEIFS=$IFS
IFS=$"\n\b"
picc=$*
if [ -f "$picc" ]; then
if [ ! -z "$tdate" ]; then
touch -t "$tdate" "$picc"
unset tdate
else
echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \e[7msuperdate\e[0m and try again."
exit 1
fi
fi
IFS=$SAVEIFS
When I do an ls -l with the filename of the file in question, it returns the date I just modified it (using a command-line executable, usually something in the ImageMagick group of apps.
Anticipating that shellcheck hasn't been used, here is the return from that utility on this script:
Code:
In /home/carver/bin/autotouch line 7:
if [ ! -z "$tdate" ]; then
^-- SC2154: tdate is referenced but not assigned.
Is the alias not passing the value? Why not? Is there a more-reliable way of assigning a string of numbers to a temporary environment variable? Should I assign such a variable elsewhere first?
What, in short, is wrong with my approach?
Typically runtime or enviroment variables are in capitals letters versus lower case. $tdate is assumed to be a regular variable and since it is not initialized in your script it is flagged as an error. It is safe to ignore the warning.
Is there some reason you need to use a environment variable?
Yes, all the shells have their own environments. Shellcheck cannot assume anything inherited from parent shell because it has no any information about that. Therefore there is no way to decide if tdate was exported or not, so the message SC2154 is ok. Also as it was mentioned aliases expanded only in interactive shells, but not in scripts.
Is there some reason you need to use a environment variable?
From Google-ing, I was led to believe environment variables were the only way to pass a variable's value from a main Terminal shell to that of a script, or from one script to another. If there's another way to do so (the former, since it's what I'm looking to do), please let me know.
As mentioned, an alias is only available to a terminal/console session.
Clarification: What is the difference between the time you're trying to load into $tdate and the current time?
One of "the others":
If the current time is not what you want, create, manually, with touch -t timestamp filename a file that will then have the time you want to use.
Then
As mentioned, an alias is only available to a terminal/console session.
Clarification: What is the difference between the time you're trying to load into $tdate and the current time?
One of "the others":
If the current time is not what you want, create, manually, with touch -t timestamp filename a file that will then have the time you want to use.
Then
Code:
touch -r "filename" "$picc"
I used your advice and had the script create, borrow from, and afterwards delete a dummy file (I even call it "dummy"). The relevant part of the script reads:
Code:
if [ ! -z "$tdate" ]; then
touch -t "tdate" dummy
touch -r "dummy" "$picc"
/home/steve/bin/trm "dummy"
unset tdate
else
echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \e[7msuperdate\e[0m and try again."
exit 1
fi
which are now lines 7 through 15.
I haven't tried running the script for a few days now. I believe a test run is due shortly.
If my alias "superdate" is not passing the value of tdate in the terminal shell, then how is it going to get to "dummy"?
Quote:
Originally Posted by L_Carver
I used your advice and had the script create, borrow from, and afterwards delete a dummy file (I even call it "dummy"). The relevant part of the script reads:
I haven't tried running the script for a few days now. I believe a test run is due shortly.
Carver
I think i should be alias-ing touch to create the dummy file, and touch that as well. I have another alias, ttouch, that executes a touch -t on the file (named in the picc variable). Should I turn that into a function that creates "dummy" every time it's invoked, and dates it with the known value of tdate, using the routine scasey suggested? (I keep noclobber off, so a frequently re-generated and re-dated "dummy" file would be no problem.)
Whould be nice to explain exactly what did you try, what happened, what did you expect and what caused confusion...
It's all elementary: the script, when I run it, is neither changing the date of the file pointed to nor creating the file "dummy" (I took out the command to move it to my temptrash folder). And the command in line 15
Code:
echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \e[7msuperdate\e[0m and try again."
isn't even returning an "empty variable" kind of error. In short, the script does nothing I can discern between running it and not running it.
If what I'm trying to do cannot be done in bash, I wish someone had said so (and suggested another way to go about it) when I started this thread. I have the strong feeling that this is the case.
it is definitely possible using bash, but you need to take care about every char, because a missing $ or { } will change the behaviour of the script.
for example in post #9
Code:
touch -t "tdate" dummy
is not what you want because a $ is missing (at least I think). Take care about those things!
As it was already mentioned use set -xv to debug your script. You will see what's happening, you can check the content of your variables and the commands as they executed.
Remember: A program will never do what you wish but what was implemented!
I put to question how or what exactly is 'tdate' and exactly how does it get assigned a value?
mod:
Code:
userx%voider ⚡ ~ ⚡> tdate
bash: tdate: command not found
unmod:
Code:
#!/bin/bash -i
SAVEIFS=$IFS
IFS=$"\n\b"
#gets month day time
tdate=$(date "+%m%d%I%S")
picc=$*
if [[ -f "$picc" && ! -z "$tdate" ]]; then
{
touch -t "$tdate" "$picc"
echo "Tdate is: $tdate set"
unset tdate
}
else
{ echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \e[7msuperdate\e[0m and try again."
exit 1
}
fi
IFS=$SAVEIFS
results
Quote:
userx%slackwhere ⚡ testing ⚡> ./dummy alleylocate
Tdate is: 05300943 set
userx%slackwhere ⚡ testing ⚡>
Quote:
Originally Posted by L_Carver
alias superdate='export tdate'
tdate is the variable to which the date string is assigned from terminal, usually something like 201601120824.46
1. But it is not showing how that export tdate gets the value into tdate to begin with - that maybe faulty.
2. off the top of my head, then instead of using $tdate you should be using $superdate otherwise that assignment is redundant because it is not being used within this script of yours. If that is why it is being exported to some var name I assume called superdate
If the purpose is to create a script to change the date on a file, I ask again: What date do you want to use that is not the current date and time? Because:
Code:
touch "$picc"
will set the date/time to the current time. It will have the essentially same effect as the script BW posted (#14). $tdate is not needed at all.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.