Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using direction operators properly and creating a month-long timer

33 views
Skip to first unread message

fred flintstone

unread,
Jan 2, 2018, 8:07:53 PM1/2/18
to
Hello NG and happy new year!

As we all ideate about how taxes will go this year, I'm doing what I
can, today, to account for work-time. Unfortunately, I've really
mucked-up my timer scripts, and what worked moderately-well 3 weeks ago
has taken a turn for the worse. Sometimes I try to do too many fancy
things, and I kill 2 scripts in a row, which really brings development
in this namespace to a stop.

$ touch 1.jan18.txt
$ ./4.timer 1.jan18.txt
basename dollar sign zero is 4.timer
path is /logs
Munged time is 2018-01-02_16:02:05
out fn is /home/bob/logs/2018-01-02_16:02:05.log
Time is 2018-01-02_16:02:05
1
2
q is jan18.txt
2.jan18.txt
61 ./4.timer errexit on line 61 dif1=$(diff --normal "$FILE" "$s" )
exit status=1
62 ./4.timer errexit on line 62 dif2=$(diff --normal "$FILE" "$s" )
>> "$d.dif.$q" exit status=1
hot damn
1a2,3
>
> I had .5 hours after I came back to the 'couve after having been away
for several days. Therein, I sorted
2 minutes and 7 seconds elapsed.
-rw-rw-r-- 1 bob bob 390 Jan 2 16:01 1.jan18.txt
-rw-rw-r-- 1 bob bob 0 Jan 2 16:04 2.dif.jan18.txt
-rw-rw-r-- 1 bob bob 498 Jan 2 16:04 2.jan18.txt
today is 2018-01-02
cat: '2018-01-02*.log': No such file or directory
witty kitty

has a tail

witty kitty

likes his ale

[big snip of output that attained through a find statement and then
redirecting]


just got toilet seat

need to install it

that's what baby wants

that's what baby gets
-------------
Here is where I start to account for time. Notice this timestamp. I will
need to many revisions of my time, given how often I will called to
account for it. For this purpose I will charge only for the time that I
have to spend talking about the work I perform. As adults, we understand
the need for to have third parties consider our situations, evaluate
them critically, even skeptically.

I had .5 hours after I came back to the 'couve after having been away
for several days. Therein, I sorted
$ cat 4.timer
#!/bin/bash
#set -eu
set -u
app=${0##*/}
#app=$(basename $0)

trap_err() {
echo " $(caller) errexit on line $1 $BASH_COMMAND exit status=$2" >&2
}

trap 'trap_err ${LINENO} $?' ERR



pathto=/logs
timename=$(date +"%Y-%m-%d_%H:%M:%S")
out=$HOME$pathto/$timename.log
SECONDS=0

echo "basename dollar sign zero is" $app
echo "path is" $pathto
echo "Munged time is" $timename
echo "out fn is $out"
echo "Time is $timename " | tee -a "$out"

declare -ir SUCCESS=0
declare -ir E_FATAL=1
MINARGS=1 # Script requires min arguments
E_BADARGS=65

# Checks number of arguments.
if [ $# -lt $MINARGS ]; then
echo "Not enough arguments."
exit $E_BADARGS
fi

FILE=$1

if [ ! -f $FILE ]; then
echo "File \"$FILE\" does not exist."
exit $E_BADARGS
fi


#get the the first number from $1
c=$(expr "$1" : '\([0-9]*\).*$')
echo $c
#integer addition
d=$(expr $c + 1)
echo $d

#munge new file, no clobber
q=${FILE#*.}
echo "q is $q" | tee -a "$out"
s="$d.$q"
echo $s
cp -n "$FILE" "$s"

gedit $s && read -n 1
duration=$SECONDS
dif1=$(diff --normal "$FILE" "$s" )
dif2=$(diff --normal "$FILE" "$s" ) >> "$d.dif.$q"
{ if [ -z "$dif1" ]
then
echo "ain't nothing there"
else
echo "hot damn"
echo "$dif1"
fi
} | tee -a "$out"
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds
elapsed." | tee -a "$out"
ls -lh *"$q" | tee -a "$out"
today=$(date +"%Y-%m-%d")
echo "today is $today" | tee -a "$out"
cat "$today*.log" | tee -a "$out"
find . -type f -name '*kitty.txt' -exec cat {} + | tee -a "$out"
echo "-------------" | tee -a "$out"
find . -type f -name "*dif.$q" -exec cat {} + | tee -a "$out"
cat $s | tee -a "$out"
gedit $out
exit $SUCCESS


#*******************end**************************

and then I just completely wash out here now:


$ ./5.timer 2.jan18.txt
basename dollar sign zero is 5.timer
path is /logs
Munged time is 2018-01-02_16:12:34
out fn is ~/logs/2018-01-02_16:12:34.log
tee: '~/logs/2018-01-02_16:12:34.log': No such file or directory
Time is 2018-01-02_16:12:34
20 ./5.timer errexit on line 20 tee -a "$out" exit status=1
2
3
tee: '~/logs/2018-01-02_16:12:34.log': No such file or directory
q is jan18.txt
50 ./5.timer errexit on line 50 tee -a "$out" exit status=1
3.jan18.txt
tee: '~/logs/2018-01-02_16:12:34.log': No such file or directory
57 ./5.timer errexit on line 57 tee -a "$out" exit status=1
58 ./5.timer errexit on line 58 dif2=$(diff --normal "$FILE" "$s" )
exit status=1
./5.timer: line 59: pathto/3.dif.jan18.txt: No such file or directory
59 ./5.timer errexit on line 59 cat $dif2 > "pathto/$d.dif.$q" exit
status=1
... more of the same...
Here is where I start to account for time. Notice this timestamp. I will
need to many revisions of my time, given how often I will called to
account for it. For this purpose I will charge only for the time that I
have to spend talking about the work I perform. As adults, we understand
the need for to have third parties consider our situations, evaluate
them critically, even skeptically.

I had .5 hours after I came back to the 'couve after having been away
for several days. Therein, I sorted a former renter's stuff out to the
cottage. I did not charge for sorting the xmas stuff into the attic,
because that is what I do as papa xmas in this casa.
75 ./5.timer errexit on line 75 tee -a "$out" exit status=1
$ cat 5.timer
#!/bin/bash
set -u
app=${0##*/}
SECONDS=0
trap_err()
{
echo " $(caller) errexit on line $1 $BASH_COMMAND exit status=$2" >&2
}
trap 'trap_err ${LINENO} $?' ERR

pathto=/logs
timename=$(date +"%Y-%m-%d_%H:%M:%S")
out=~$pathto/$timename.log
SECONDS=0

echo "basename dollar sign zero is" $app
echo "path is" $pathto
echo "Munged time is" $timename
echo "out fn is $out"
echo "Time is $timename " | tee -a "$out"

declare -ir SUCCESS=0
declare -ir E_FATAL=1
MINARGS=1 # Script requires min arguments
E_BADARGS=65

# Checks number of arguments.
if [ $# -lt $MINARGS ]; then
echo "Not enough arguments."
exit $E_BADARGS
fi

FILE=$1

if [ ! -f $FILE ]; then
echo "File \"$FILE\" does not exist."
exit $E_BADARGS
fi


#get the first number from $1
c=$(expr "$1" : '\([0-9]*\).*$')
echo $c
#integer addition
d=$(expr $c + 1)
echo $d

#munge new file, no clobber
q=${FILE#*.}
echo "q is $q" | tee -a "$out"
s="$d.$q"
echo $s
cp -n "$FILE" "$s"

gedit $s && read -n 1
duration=$SECONDS
dif1=$(diff --normal "$FILE" "$s" ) | tee -a "$out"
dif2=$(diff --normal "$FILE" "$s" )
cat $dif2 > "pathto/$d.dif.$q"
{ if [ -z "$dif1" ]
then
echo "ain't nothing there"
else
echo "hot damn"
echo "$dif1"
fi
} | tee -a "$out"
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds
elapsed." | tee -a "$out"
ls -lh *"$q" | tee -a "$out"
today=$(date +"%Y-%m-%d")
echo "today is $today" | tee -a "$out"
cd "~$pathto"
cat "$today"* | tee -a "$out"
echo "-------------" | tee -a "$out"
cat $s | tee -a "$out"
gedit $out
exit $SUCCESS

#*******************end**************************
$

Alright, so it's a hot mess right now. What do I want out of this?

1) execution without unhandled errors

2) an understanding of why dif2 never attains. The idea is that I want
to create a new file that is the output of whatever was the difference
between one version of a document and its clone, after the later has
been extended or revised. Then I want to keep

3) a running statistic adding a value from a line in the logs

such that it matches 2018-01 in the title

and then the script has a line where it says

Duration=...

I don't like the way it's echoed in 5.timer and seek something ISO.

4) Why do I not get an outfile from 5.timer?

5) Can I blame the Dino?

Thanks for your comment, and if you are already dour, you need to go get
laid....
--

fred flintsone

Bit Twister

unread,
Jan 2, 2018, 11:12:19 PM1/2/18
to
On Tue, 2 Jan 2018 17:07:45 -0800, fred flintstone wrote:
>
>
> Alright, so it's a hot mess right now. What do I want out of this?
>
> 1) execution without unhandled errors

Not sure what you mean about that. Errors can happen at anytime.
For example, you run out of space after writing the first line to the
output file.

> 2) an understanding of why dif2 never attains. The idea is that I want
> to create a new file that is the output of whatever was the difference
> between one version of a document and its clone, after the later has
> been extended or revised. Then I want to keep

Personally, I despise you diff methodology. My suggestion:
diff --normal "$FILE" "$s" > $_dif_fn
if [ $? -ne 0 ] ; then
cat $_dif_fn >> $_log_fn
echo "hot damn"
cat $_dif_fn
else
echo "ain't nothing there"
fi
rm --force $_dif_fn

> 3) a running statistic adding a value from a line in the logs
> such that it matches 2018-01 in the title

For things like that you output the keyword=value to the log, then do
something like eval=$(grep keyword= $_log_fn)
at this point, keyword will have the value from the log file.

> and then the script has a line where it says
>
> Duration=...
>
> I don't like the way it's echoed in 5.timer

Then you get to do the math to convert your minutes back to seconds
and subtract to get the remainder in seconds.

> and seek something ISO.

I recommend you feed your duration through "date" if you want a
reliable output value.

> 4) Why do I not get an outfile from 5.timer?

Why do you not read the error message. Here take a closer look.

./5.timer: line 59: pathto/3.dif.jan18.txt: No such file or directory

I would also recommend always using $HOME instead of ~.

You might want to consider changing cp -n to cp --no-clobber just for clarity
around line 53.

fred flintstone

unread,
Jan 4, 2018, 10:17:07 AM1/4/18
to
Ok, thx, bt, I haven't gotten to everything above, but I did get the dif
functionality working. What I'm working on now is a bit of a work-around
to get scripts to display better. Here's what I have now:

$ cat 3.indent
#!/bin/bash
set -u
app=${0##*/}
trap_err()
{
echo " $(caller) errexit on line $1 $BASH_COMMAND exit status=$2" >&2
}
trap 'trap_err ${LINENO} $?' ERR

pathto=/logs
timename=$(date +"%m-%d-%Y-%H-%M-%S")
out=$HOME$pathto/$timename.log
#new for indent
cp -n "$FILE" "$FILE.bak"
## pretty print bash
source <(echo "Zibri () {";cat $FILE.bak; echo "}")
declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3

gedit $s && read -n 1
duration=$SECONDS

#done editing

diff --normal "$FILE" "$s" > "$d.$q.dif"
if [ $? -ne 0 ] ; then
cat $d.$q.dif >> $out
echo "hot damn"

else
echo "ain't nothing there"
fi

ls -lh *"$q"* | tee -a "$out"
today=$(date +"%Y-%m-%d")
echo "today is $today" | tee -a "$out"



gedit $out
exit $SUCCESS
$

What I'm trying to do here is to take these 2 lines

## pretty print bash
source <(echo "Zibri () {";cat $FILE.bak; echo "}")
declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3

and have them redirected to "$s" .

I do not understand the above 2 lines, but they seem to format bash
scripts on stdout. What is Zibri? How can the declare statement be after
the source statement and still work?
--
fred

Bit Twister

unread,
Jan 4, 2018, 10:48:04 AM1/4/18
to
On Thu, 4 Jan 2018 07:16:57 -0800, fred flintstone wrote:
>
> What I'm trying to do here is to take these 2 lines
>
> ## pretty print bash
> source <(echo "Zibri () {";cat $FILE.bak; echo "}")
> declare -f Zibri| cut -c 5-|head --lines=-1|tail --lines=+3
>
> and have them redirected to "$s" .
>
> I do not understand the above 2 lines, but they seem to format bash
> scripts on stdout. What is Zibri?

A function. As you can see being a lazy coder leads to a harder task
to show what is going on. I always use the "function" keyword on all
my function declarations. Also makes it easy to use grep to get a list
of functions in scripts. I also put a comment at the function end to
help speed reading/scrolling through the code. Example snipped:

#! /bin/bash
. /local/bin/functions_path_app

set -u # Show usage of unset variables as an error to help catch typos.
<snip>
function spawn_editor ()
{
if [ -n "$_fn" ] ; then
_type=${_fn#*.}
else
_type=""
fi

case $_type in
pdf) _app=pedt ;;
ods) libreoffice --calc $_fn &
exit 0
;;
odt) libreoffice $_fn &
<big snip>
nedt)
nedit $_fn &
;;
epdf)
evince $_fn 2>/dev/null &
;;
*)
echo "Unknown exe name $_app"
exit 1
;;
esac
sleep 1
} # end function spawn_editor



> How can the declare statement be after the source statement and
> still work?

The "source" command adds the contents to the current bash session.

declare sets the variable from the function's output.

Janis Papanagnou

unread,
Jan 4, 2018, 11:37:46 AM1/4/18
to
On 04.01.2018 16:47, Bit Twister wrote:
>> [...]
>
> A function. As you can see being a lazy coder leads to a harder task
> to show what is going on. I always use the "function" keyword on all
> my function declarations. Also makes it easy to use grep to get a list
> of functions in scripts. [...]

In ksh there's 'typeset +f' to see all function names and 'typeset -f'
to see all function definitions, and 'typeset -f funname' to see the
definition of function funname. Bash seems to have adopted 'typeset -f'
and 'typeset -f funname'. And, as opposed to grep, 'typeset -f' will
not list functions that are commented out, or conditionally defined,
etc.

Janis

Janis Papanagnou

unread,
Jan 4, 2018, 11:52:15 AM1/4/18
to
Erm, to apply that you need to execute the script of course (which may
not be intended for a plain text search).

> Janis
>

fred flintstone

unread,
Jan 4, 2018, 6:57:53 PM1/4/18
to
On 01/02/2018 08:12 PM, Bit Twister wrote:
> On Tue, 2 Jan 2018 17:07:45 -0800, fred flintstone wrote:

>> 2) an understanding of why dif2 never attains. The idea is that I want
>> to create a new file that is the output of whatever was the difference
>> between one version of a document and its clone, after the later has
>> been extended or revised. Then I want to keep
>
> Personally, I despise you diff methodology. My suggestion:
> diff --normal "$FILE" "$s" > $_dif_fn
> if [ $? -ne 0 ] ; then
> cat $_dif_fn >> $_log_fn
> echo "hot damn"
> cat $_dif_fn
> else
> echo "ain't nothing there"
> fi
> rm --force $_dif_fn

$ ./11.blu
basename dollar sign zero is 11.blu
path is /logs
Munged time is 01-04-2018-15-35-27
out fn is /home/bob/logs/01-04-2018-15-35-27.log
before is without device
hook up unlocked phone
Smash any key to continue...
ain't nothing there
Time is 01-04-2018-15-35-27
mount diff data
Че те надо
--------------
--------------
-- Logs begin at Sun 2017-12-31 17:20:07 PST, end at Thu 2018-01-04
15:35:38 PST. --

...
org.gtk.vfs.GPhoto2VolumeMonitor[1498]: (process:2042):
GVFS-GPhoto2-WARNING **: device (null) has no BUSNUM property, ignoring
Jan 04 15:34:24 bob-ThinkPad-SL510 kernel: usb 2-1: USB disconnect,
device number 26
Jan 04 15:34:25 bob-ThinkPad-SL510 org.gtk.vfs.Daemon[1498]: Android
device detected, assigning default bug flags
Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: new high-speed USB
device number 27 using ehci-pci
Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: New USB device
found, idVendor=0e8d, idProduct=2008
Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: New USB device
strings: Mfr=2, Product=3, SerialNumber=4
Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: Product: Advance 5.0
Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: Manufacturer: BLU
Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: SerialNumber:
0123456789ABCDEF
Jan 04 15:35:37 bob-ThinkPad-SL510 mtp-probe[29894]: checking bus 2,
device 27: "/sys/devices/pci0000:00/0000:00:1d.7/usb2/2-1"
Jan 04 15:35:37 bob-ThinkPad-SL510 mtp-probe[29894]: bus: 2, device: 27
was an MTP device
Jan 04 15:35:37 bob-ThinkPad-SL510 org.gtk.vfs.Daemon[1498]: PTP:
reading event an error 0x02ff occurredDevice 0 (VID=0e8d and PID=2008)
is a MediaTek Inc MT65xx.
Jan 04 15:35:38 bob-ThinkPad-SL510 gnome-session[1671]:
(nautilus:29923): GLib-GIO-CRITICAL **:
g_dbus_interface_skeleton_unexport: assertion
'interface_->priv->connections != NULL' failed
Jan 04 15:35:38 bob-ThinkPad-SL510 gnome-session[1671]:
(nautilus:29923): GLib-GIO-CRITICAL **:
g_dbus_interface_skeleton_unexport: assertion
'interface_->priv->connections != NULL' failed
Jan 04 15:35:38 bob-ThinkPad-SL510 gnome-session[1671]:
(nautilus:29923): Gtk-CRITICAL **: gtk_icon_theme_get_for_screen:
assertion 'GDK_IS_SCREEN (screen)' failed
Jan 04 15:35:38 bob-ThinkPad-SL510 gnome-session[1671]:
(nautilus:29923): GLib-GObject-WARNING **: invalid (NULL) pointer instance
Jan 04 15:35:38 bob-ThinkPad-SL510 gnome-session[1671]:
(nautilus:29923): GLib-GObject-CRITICAL **: g_signal_connect_object:
assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
Jan 04 15:35:38 bob-ThinkPad-SL510 dbus[836]: [system] Activating via
systemd: service name='org.freedesktop.hostname1'
unit='dbus-org.freedesktop.hostname1.service'
Jan 04 15:35:38 bob-ThinkPad-SL510 systemd[1]: Starting Hostname Service...
Jan 04 15:35:38 bob-ThinkPad-SL510 dbus[836]: [system] Successfully
activated service 'org.freedesktop.hostname1'
Jan 04 15:35:38 bob-ThinkPad-SL510 systemd[1]: Started Hostname Service.
Jan 04 15:35:38 bob-ThinkPad-SL510 gnome-session[1671]:
(nautilus:15038): Gtk-WARNING **: Refusing to add non-unique action
'TerminalNautilus:OpenFolderLocal' to action group 'ExtensionsMenuGroup'
Jan 04 15:35:38 bob-ThinkPad-SL510
org.gtk.vfs.GPhoto2VolumeMonitor[1498]: (process:2042):
GVFS-GPhoto2-WARNING **: device (null) has no BUSNUM property, ignoring
duration=18
$

$ cat 11.blu
#!/bin/bash
set -u
app=$(basename $0)
pathto=/logs
timename=$(date +"%m-%d-%Y-%H-%M-%S")
out=$HOME$pathto/$timename.log
SECONDS=0


echo "basename dollar sign zero is" $app
echo "path is" $pathto
echo "Munged time is" $timename
echo "out fn is $out"
echo "Time is $timename " >> $out
echo "mount diff data" >> $out
echo "Че те надо" >> $out
cd $HOME/1.scripts
mount > 3.before.txt
echo "before is without device"
echo -en "hook up unlocked phone\nSmash any key to continue..." && read -n 1
# do some work
mount > 3.after.txt
echo "--------------" >> $out
diff --normal "3.before.txt" "3.after.txt" >> $out
if [ $? -ne 0 ] ; then
cat $_dif_fn >> $out
echo "hot damn"
cat $_dif_fn
else
echo "ain't nothing there"
fi
echo "--------------" >> $out

journalctl --since "15 minutes ago" >> $out

duration=$SECONDS
echo "duration=$duration">> $out
cat $out
gedit $out
exit
#*******************end**************************
$

The good news is that the dif works. The bad news is that there's no
difference in the mount data before and after the phone is hooked up.
This, too, is a bit of a diversion from the subject of thread. I try to
believe otherwise every time I talk to you, bt; it just doesn't change
the data. The suggestion that I might lie about such a thing I found
bemusing.
--
fred flintstone

Bit Twister

unread,
Jan 4, 2018, 7:56:58 PM1/4/18
to
On Thu, 4 Jan 2018 15:57:46 -0800, fred flintstone wrote:
> ...
> org.gtk.vfs.GPhoto2VolumeMonitor[1498]: (process:2042):
> GVFS-GPhoto2-WARNING **: device (null) has no BUSNUM property, ignoring
> Jan 04 15:34:24 bob-ThinkPad-SL510 kernel: usb 2-1: USB disconnect,
> device number 26

Guessing that was you unplugging the phone.
I have seen bugs in the past where second event plugin was ignored by
the system.



> Jan 04 15:34:25 bob-ThinkPad-SL510 org.gtk.vfs.Daemon[1498]: Android
> device detected, assigning default bug flags
> Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: new high-speed USB
> device number 27 using ehci-pci
> Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: New USB device
> found, idVendor=0e8d, idProduct=2008
> Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: New USB device
> strings: Mfr=2, Product=3, SerialNumber=4
> Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: Product: Advance 5.0
> Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: Manufacturer: BLU
> Jan 04 15:35:37 bob-ThinkPad-SL510 kernel: usb 2-1: SerialNumber:
> 0123456789ABCDEF

There is indication system sees the device and I gave you a link which
suggests you might need to create a udev rule to help manage the
device mount.


> Jan 04 15:35:37 bob-ThinkPad-SL510 mtp-probe[29894]: checking bus 2,
> device 27: "/sys/devices/pci0000:00/0000:00:1d.7/usb2/2-1"

I see you still have that extra mtp package installed. :(
I suggest removing it to keep things simple.


> The good news is that the dif works. The bad news is that there's no
> difference in the mount data before and after the phone is hooked up.

Yep, but awhile back I ask to see what was in /home/bob/media (I can't
remember the rest) while the phone was plugged in and did not receive
a reply.


I suggest you get into your Gnome device manager and see if you can
find a setting to allow you to auto-mount removable devices.
I do not run Gnome so I can not help you there. I also do not run
*buntu so there may not be such a selection.

Just for fun, I would also add the disk group to bob's ID, then log
out/in, and diff mount results from before/after phone connect.


I also asked you to run a mount diff while you are able to browse the
phone contents with your gui file manger.

I still want to see that.
0 new messages