LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automate "touch" bash script not working. (https://www.linuxquestions.org/questions/linux-newbie-8/automate-touch-bash-script-not-working-4175606382/)

L_Carver 05-21-2017 09:19 AM

Automate "touch" bash script not working.
 
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?

Carver

michaelk 05-21-2017 09:58 AM

Quote:

ShellCheck does not attempt to figure out runtime or dynamic assignments
https://github.com/koalaman/shellcheck/wiki/SC2154

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?

AwesomeMachine 05-21-2017 12:24 PM

The shell script has its own shell. I'm not sure if it is aware of your aliases. You might want to just type it all out in the script.

pan64 05-22-2017 12:52 AM

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.

L_Carver 05-23-2017 08:09 AM

Quote:

Originally Posted by michaelk
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.

Carver

pan64 05-23-2017 08:17 AM

in general there are 3 ways:
1. environment variable
2. command line parameter
3. (config) file
4. probably there are others too

michaelk 05-23-2017 08:28 AM

It depends on your application(s) and how they are called. There are several methods like runtime, command line arguments or configuration files.

Great minds think alike...

scasey 05-23-2017 12:44 PM

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"

L_Carver 05-27-2017 11:48 PM

Quote:

Originally Posted by scasey (Post 5714300)
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.

Carver

L_Carver 05-28-2017 02:57 PM

Something confuses me here.
 
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 (Post 5716096)
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.)

Carver

pan64 05-29-2017 02:58 AM

Quote:

Originally Posted by L_Carver (Post 5716280)
If my alias "superdate" is not passing the value of tdate in the terminal shell, then how is it going to get to "dummy"?

Whould be nice to explain exactly what did you try, what happened, what did you expect and what caused confusion...

L_Carver 05-30-2017 01:47 AM

Quote:

Originally Posted by pan64 (Post 5716432)
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.

Carver

pan64 05-30-2017 02:58 AM

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!

BW-userx 05-30-2017 09:27 AM

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

scasey 05-30-2017 12:01 PM

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.


All times are GMT -5. The time now is 02:20 PM.