LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-21-2017, 09:19 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
Question 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
 
Old 05-21-2017, 09:58 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
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?

Last edited by michaelk; 05-21-2017 at 10:19 AM.
 
Old 05-21-2017, 12:24 PM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
Old 05-22-2017, 12:52 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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.
 
Old 05-23-2017, 08:09 AM   #5
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-23-2017, 08:17 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
in general there are 3 ways:
1. environment variable
2. command line parameter
3. (config) file
4. probably there are others too
 
Old 05-23-2017, 08:28 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
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...
 
Old 05-23-2017, 12:44 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,708

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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"
 
Old 05-27-2017, 11:48 PM   #9
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
Wink

Quote:
Originally Posted by scasey View Post
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
 
Old 05-28-2017, 02:57 PM   #10
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
Unhappy 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 View Post
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
 
Old 05-29-2017, 02:58 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
Quote:
Originally Posted by L_Carver View Post
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...
 
Old 05-30-2017, 01:47 AM   #12
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
Exclamation

Quote:
Originally Posted by pan64 View Post
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

Last edited by L_Carver; 05-30-2017 at 01:50 AM.
 
Old 05-30-2017, 02:58 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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!
 
Old 05-30-2017, 09:27 AM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 05-30-2017 at 09:51 AM.
 
Old 05-30-2017, 12:01 PM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,708

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell Script: "bash: Bad Substitution - Script for remove "." " thiagofw Programming 14 12-09-2016 10:04 PM
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 07:51 PM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
command " cd .. " not working in bash script? Kilam orez Linux - Newbie 7 03-12-2010 04:14 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration