LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script suddenly stopped working (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-suddenly-stopped-working-4175606794/)

pan64 06-11-2017 03:44 AM

set -xv is not error checking, but debugging. That means the shell will print a lot of information about what's happening. But that will not check anything for you. Instead, you need to check yourself if the program flow is what you expected or not.
Again you did not post what you really tried therefore noone will tell you what's wrong with that.

Additionally if you post an output produced using set -xv we can go through to understand line by line.

L_Carver 06-12-2017 12:46 PM

Current script is attached; output of ...
 
1 Attachment(s)
...running the script with set -xv (line 2) and ls dummy (right after) looks like:
Code:

farragut/public $ autotouch $picc
The empty file, dummy, is now dated as follows
2013-12-18 05:18:06.000000000 -0500
farragut/public $ ls dummy
dummy

The command in the script to remove dummy is now
Code:

[[ -f "dummy" ]] && rm -f dummy
You have what I have. Now where can I go?

Carver

ntubski 06-12-2017 04:20 PM

Quote:

Originally Posted by L_Carver (Post 5721983)
...running the script with set -xv (line 2) and ls dummy (right after) looks like:

Here's what it looks like for me:
Code:

$ ./autotouch.sh words.txt
SAVEIFS=$IFS
+ SAVEIFS='
'
IFS=$"\n\b"
+ IFS='\n\b'

picc=$1
+ picc=words.txt
if [ -f "$picc" ]; then
    if [ ! -z "$TDATE" ]; then
        touch dummy
        touch -t "$TDATE" dummy
        echo "The empty file, dummy, was dated as follows"
        stat -c %y dummy
        touch -r "dummy" "$picc"
        [[ -f "dummy" ]] && rm -f dummy
    fi
    /home/steve/bin/nodummy
    IFS=$SAVEIFS
./autotouch.sh: line 18: syntax error: unexpected end of file

Quote:

You have what I have. Now where can I go?
Try to work out the difference between what you posted and what you actually ran (also, we don't have /home/steve/bin/nodummy).

L_Carver 06-16-2017 12:47 PM

1 Attachment(s)
Quote:

Originally Posted by ntubski (Post 5722066)
Try to work out the difference between what you posted and what you actually ran (also, we don't have /home/steve/bin/nodummy).

Sorry. I thought I'd either pasted "nodummy" in or uploaded it. Solving that (non)problem now. As the uploaded version shows, it's just "hashbang, check that dummy exists, if so remove it, if not say so" as a separate script. Basically I don't see why I can't do the remove-it part in "autotouch" (the longer, parent script), and that's the modification I'm having trouble with.

And I did not get the error you did when you ran "autotouch." I must have fixed it after posting and thus the non-working version was prone to err out. Here's the (error-free) version of the script as it ran today:
Code:

#!/bin/bash
set -xv
SAVEIFS=$IFS
IFS=$'\n\b'

picc=$1
if [ -f "$picc" ]; then
if [ ! -z "$TDATE" ]; then
 touch dummy
 touch -t "$TDATE" dummy
 echo "The empty file, dummy, was dated as follows"
 stat -c %y dummy
 touch -r "dummy" "$picc"
[[ -f "dummy" ]] && rm -f dummy
fi
/home/steve/bin/nodummy
IFS=$SAVEIFS

What I can't explain is that the 'set -xv' doesn't return any of the executed commands to stdout. And that was what i "reported" in my post of the 10th @ 6:05 AM.

Carver

pan64 06-16-2017 12:56 PM

Quote:

Originally Posted by L_Carver (Post 5723479)
What I can't explain is that the 'set -xv' doesn't return any of the executed commands to stdout. And that was what i "reported" in my post of the 10th @ 6:05 AM.

Carver

I would rather say those commands were really not executed. By the way set -xv sends its output to stderr, not stdout.

scasey 06-16-2017 01:04 PM

Quote:

Originally Posted by L_Carver (Post 5723479)
Code:

[[ -f "dummy" ]] && rm -f dummy

Wouldn't just
Code:

rm -f dummy
work? You shouldn't need to test for the presence of the file dummy when using rm -f [force]. The force will suppress any error reporting -- you won't get a "No such file or directory" from rm.
Code:

[scasey:~]$ rm dummy
rm: cannot lstat `dummy': No such file or directory
[scasey:~]$ rm -f dummy
[scasey:~]$

Please try that and let us know.

rknichols 06-16-2017 01:20 PM

Quote:

Originally Posted by L_Carver (Post 5716092)
As for going with single quotes on the
Code:

IFS=$"\n\b"
I may try that, too. Double-quotes with me are reflections of my Cygwin days, and I usually "break" a bash install with them (as with mv and renaming single files) by using them.

You had better learn to do it right or you will have no end of trouble. The two are very different, especially when the quoted string is preceded by "$".

From the bash manpage:
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
and
A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored.
Presumably, the first is what you wanted, though <newline> and <backspace> are a rather odd combination for field separators.

ntubski 06-16-2017 02:24 PM

Quote:

Originally Posted by L_Carver (Post 5723479)
And I did not get the error you did when you ran "autotouch." I must have fixed it after posting and thus the non-working version was prone to err out. Here's the (error-free) version of the script as it ran today:
Code:

#!/bin/bash
set -xv
SAVEIFS=$IFS
IFS=$'\n\b'

picc=$1
if [ -f "$picc" ]; then
if [ ! -z "$TDATE" ]; then
 touch dummy
 touch -t "$TDATE" dummy
 echo "The empty file, dummy, was dated as follows"
 stat -c %y dummy
 touch -r "dummy" "$picc"
[[ -f "dummy" ]] && rm -f dummy
fi
/home/steve/bin/nodummy
IFS=$SAVEIFS


Nope, that still has the same error: there are 2 if and only 1 fi.

Quote:

What I can't explain is that the 'set -xv' doesn't return any of the executed commands to stdout. And that was what i "reported" in my post of the 10th @ 6:05 AM.
What does
Code:

which autotouch
tell you? I guess the file you are editing is not the same as the file you are running.

L_Carver 06-17-2017 07:54 AM

Following latest advice...
 
...I made these changes to the body of the script (leave off, for the moment, my IFS declaration.):
Code:

rm -f dummy
fi
else
 echo -e "The file $picc is not in this directory.\nHas it been moved?"
fi
# Just in case that bit hiccups again:
/home/steve/bin/nodummy
IFS=$SAVEIFS

And per the double-quote around "/n/b," I had it happen while editing another script that changing " to ' resulted in an unmatched " kind of error, so I changed them back.

Carver

L_Carver 06-17-2017 08:33 AM

single quotes '/n/b' work, but...
 
...the commands to delete dummy are still ignored, as is, it appears, the set -xv command.
I copied the script with the name 'autodate,' and set -xv works fine in it.

Carver

pan64 06-17-2017 10:30 AM

Quote:

Originally Posted by L_Carver (Post 5723739)
...the commands to delete dummy are still ignored
Carver

You need to understand, the program will do what is implemented, not what you wish. Commands cannot be ignored, there is no such feature and there is no similar bug in bash. The only thing I can imagine the program flow is simply different: it either stopped before - or run on another branch (if/then/else).
set -xv works always, if used, that will not influence the program flow anyway but print the lines as they executed.

You need to check if there was any error message.

L_Carver 06-18-2017 03:39 AM

Quote:

Originally Posted by pan64 (Post 5723782)
You need to understand, the program will do what is implemented, not what you wish. Commands cannot be ignored, there is no such feature and there is no similar bug in bash. The only thing I can imagine the program flow is simply different: it either stopped before - or run on another branch (if/then/else).
set -xv works always, if used, that will not influence the program flow anyway but print the lines as they executed.

You need to check if there was any error message.

There are no error messages. I see the script executing step-by-step when I run autodate, the copy of the working script (as I should with set -xv on line 4), but I do not see it execute step-by-step in the original script, autotouch. After running autodate, I've noticed from recent runs, the modified date of the file it was supposed to touch with the value in tdate does not change, but dummy is still deleted. When I run autotouch, the mod date changes but dummy isn't deleted (the issue for the second half of this thread). a-date not changing the date of the target file is bad; a-touch leaving dummy behind (and on top of that, neither checking if there is a dummy file nor executing nodummy) is also bad.

Each script has one or two parts that "don't work," and I've been trying to figure out why.

I'm trying to remove the 'old' file dummy in anticipation of situations where I may want to change the mod date of a file but noclobber IS set. I would like to set it on my own system at some
point and still have this/these script/s work. If there's a 'touch' command that creates a new file dummy every time either the first or second script is executed, that won't fly.

Carver

pan64 06-18-2017 05:44 AM

so it looks like you execute different files (not the one you posted), execute something with different names or in different directories and checks something, but you never tell us what was really executed, what's happened exactly and what was expected. Please post exactly the full script you executed, the full output generated (exactly) and do not try to cut/explain modify them, just post as it is.
Otherwise it looks completely pointless (at least for me)

L_Carver 06-18-2017 11:15 AM

What color is the egg on my face now?
 
Quote:

Originally Posted by pan64 (Post 5723994)
so it looks like you execute different files (not the one you posted), execute something with different names or in different directories and checks something, but you never tell us what was really executed, what's happened exactly and what was expected. Please post exactly the full script you executed, the full output generated (exactly) and do not try to cut/explain modify them, just post as it is.
Otherwise it looks completely pointless (at least for me)

What is expected is: the script will "touch" the target file with the -t type string value in the variable tdate, then delete the dummy file it creates to pass this along to. I read earlier in this thread or the earlier thread (on trimming down the code for this script) that one of the better ways to pass a modification date to a new file with a script was to create a dummy file and then copy its modification date to the "new" file.

The ways I execute (consistently from the time I wrote the first version of this script to the date I'm writing this reply) have either been
Code:

autotouch $picc
OR
Code:

autotouch foo.jpg
Now I've discovered that the script files are not the problem. It turns out I wrote the commands of the original autotouch into a function in my .bash_aliases file, which looks like this:
Code:

function autotouch () {

if [[ -f dummy ]]; then rm dummy; fi
if [ ! -z "$tdate" ]; then
touch dummy
 touch -t "$tdate" dummy
 echo "The empty file, dummy, is now dated as follows"
 stat -c %y dummy
 touch -r "dummy" "$picc"
fi
[[ -f dummy ]]; rm dummy
}

And coincidentally, when executed, the "rm dummy" does not remove the dummy file.

Carver

scasey 06-18-2017 02:38 PM

Quote:

Originally Posted by L_Carver (Post 5723974)
If there's a 'touch' command that creates a new file dummy every time either the first or second script is executed, that won't fly.

touch will always create a new file if the filename doesn't exist when it is run. So the first command below is unnecessary (but won't hurt anything)
Code:

touch dummy
 touch -t "$tdate" dummy

Quote:

Originally Posted by pan64 (Post 5723994)
so it looks like you execute different files (not the one you posted), execute something with different names or in different directories and checks something, but you never tell us what was really executed, what's happened exactly and what was expected. Please post exactly the full script you executed, the full output generated (exactly) and do not try to cut/explain modify them, just post as it is.
Otherwise it looks completely pointless (at least for me)

What pan64 said. Emphasis is mine.


All times are GMT -5. The time now is 08:03 PM.