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' The script looks like this: Code:
#!/bin/bash -i 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: What, in short, is wrong with my approach? Carver |
Quote:
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? |
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.
|
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.
|
Quote:
Carver |
in general there are 3 ways:
1. environment variable 2. command line parameter 3. (config) file 4. probably there are others too |
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... |
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" |
Quote:
Code:
if [ ! -z "$tdate" ]; then I haven't tried running the script for a few days now. I believe a test run is due shortly. Carver |
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:
Carver |
Quote:
|
Quote:
Code:
echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \e[7msuperdate\e[0m and try again." 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 |
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 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 Code:
#!/bin/bash -i Quote:
Quote:
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" |
All times are GMT -5. The time now is 09:36 AM. |