#StackBounty: #command-line #bash #scripts #inotify Adding script bash variables to awk and inotify

Bounty: 50

I want to create a script that will log every access of a directory or any file from that directory during a day, for that, I use inotifywait, but I don’t like the output even though i formated it, I need the user that accessed/modified the file as well. And I want to print it in the table format. Something like this:

TIME               USER     FILE            EVENT
%mm:%HH PM/am      root     /home/root/x    Accesed(or anything the inotifywait gives)

And I tried something like this:

#!/bin/sh

watchedDir=$1
logFileName="$(date +'%d.%m.%Y').log"

iwait() {
    inotifywait -r -m --timefmt "%Y/%m/%d %H:%M:%S" --format "%T;%w%f;%e" $watchedDir >> "$PWD/.$logFileName.tmp"
}

write_to_file() {
    while true; do
    last_entry=$(tail -n 1 "$PWD/$logFileName.tmp")
    time=$(tail -n 1 "$PWD/$logFileName.tmp" | cut -f1 -d';')
    user=$(stat $last_entry --format="%U")
    file=$(tail -n 1 "$PWD/$logFileName.tmp" | cut -f2 -d';')
    event=$(tail -n 1 "$PWD/$logFileName.tmp" | cut -f3 -d';')

    awk -v time="$time" -v user="$user" -v file="$file" -v event="$event" 'BEGIN {printf("%s %8s %8s %8s n" ,"Time", "User", "File", "Event")}
    {printf("%s %s %s %sn", time, user, file, event)}' >> "$PWD/.$logFileName.tmp"
    done
}

if [ "$(realpath $watchedDir)" != "$PWD" ]
then
    iwait &
    write_to_file &
    wait
fi

I also found out that if I try to watch the current directory and also redirect the file into the current directory it will flood the ouput… so I tried to get over that using that if.

How can I do something like that?


Get this bounty!!!

#StackBounty: #bash #shell #zsh #oh-my-zsh #command-history How to setup zsh to append history to a file named dynamically by date

Bounty: 50

My biggest sticking point moving from bash to zsh is the history configuration. Using oh-my-zsh there is a history.zsh file in #ZSH/lib that I found (I think) can be disabled via unsetopt share_history in .zshrc.

I want to just mimic my bash history setup that uses PROMPT_COMMAND bash env that runs after each interactive action (command entered). I found here that zsh has a hook called precmd that is somewhat similar.

Below is my custom bash history setup that writes history to the ~/.logs directory in files of the current date, i.e. bash-history-2020-10-27.log


    # Saving history to file
    export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'
    
    export HISTSIZE=100000
    export HISTTIMEFORMAT="%d/%m/%y %T  "
    # Avoid duplicates
    export HISTCONTROL=ignoredups:erasedups  
    # When the shell exits, append to the history file instead of overwriting it
    #shopt -s histappend
    
    # After each command, append to the history file and reread it
    export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'n'}history -a; history -c; history -r"

The modified zsh version is shown below. This config lives in the $ZSH_CUSTOM directory.


    # Saving history to file
    setopt INC_APPEND_HISTORY
    setopt EXTENDED_HISTORY
    setopt HIST_FIND_NO_DUPS
    setopt HIST_IGNORE_ALL_DUPS
    unsetopt share_history
    
    export HISTSIZE=1000000000
    export HISTFILESIZE=1000000000
    export HISTTIMEFORMAT="%d/%m/%y %T  "
    
    # Functions
    precmd() { eval 'if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/zsh-history-$(date "+%Y-%m-%d").log; fi' }

Which certainly does write to the .logs directory in a file named the date however my timestamp formatting is not being leveraged. Futhermore I suspect that the whole .zsh_history file is just being written and not incrementally the last command entered in the shell.

Curious if anyone more familiar with zsh & oh-my-zsh configs can help me? It would much appreciated! I love the flow of having timestamp shell logs that I then use an aliased function to search over. In case anyone is interested here is the one I use in my bash setup:


    alias s='search'
    search() {
       ls -rt ~/.logs/*.log | xargs grep -rnw "$1"
    }

Also curious to hear about alternative zsh history setups that you would recommend and does a better jobs segmenting by date. Thank you!


Get this bounty!!!

#StackBounty: #bash #shell #zsh #autocomplete How does Bash/Zsh identify the relevant command during completion?

Bounty: 50

When I start typing something into an interactive login shell (zsh or bash) and click tab, the shell offers me autocomplete suggestions.

I broadly understand how completion specs are defined. For reference it’s outlined here:

What I don’t understand is how bash/zsh identify which command to complete on when a user presses tab i.e. if I do ls [tab], how does bash/zsh identify ls. Put differently, how do bash/zsh know to generate completions using the ls completion spec.

Some more examples with more complicated parsing which Bash/Zsh still get right:

  • ls [tab] -> completes on ls
  • echo $( echo $(ls [tab] -> completes on ls
  • echo hi; git a[tab] -> completes on git
  • echo 'cd [tab] -> does NOT complete on cd
  • echo "$(ls [tab] -> completes on ls
  • echo $( echo hi | ls [tab] -> completes on ls
    (I know ls doesn’t take stdin but this example still illustrates bash/zsh’s parsing abilities)

I am presuming bash/zsh are using some sort of parser. However, it’s not a normal parser. It completes without a properly structured command. It is aware that something is in quotes or a shell expansion, even if they are not closed.

What function is bash/zsh running to "identify" the relevant command or determine whether or not there is a command to complete on at all?


Get this bounty!!!

#StackBounty: #bash #functions A function can only be called if its name is the first word of an expression?

Bounty: 50

A function can only be called if its name is the first word of an expression, as is the case with aliases?
Or can it also be the nth word of an expression?
I can’t emulate a use case that answers this question of mine.
I can’t find a guide that answers this question of mine.
Being Italian, I find it difficult to find the right words on the Google search engine.
Can you also mention the source in the answer?
Thank you.


Get this bounty!!!

#StackBounty: #linux #bash #centos #python3 #sqlite ModuleNotFoundError: No module named '_sqlite3'

Bounty: 50

We have different python version installed and specifically using python3.7 so I have edited my .bashrc file. We are using Centos7 with Linux server.

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
alias python=python3.7
alias pip=pip3.7


[xyz@innolx20122 ~]$ python
python             python2.7          python3.6          python3.7          python3.7m-config
python2            python3            python3.6m         python3.7m

[xyz@innolx20122 ~]$ which sqlite3
/usr/bin/sqlite3

Its working with python2.7 and python3.6 version

[xyz@innolx20122 ~]$ python2.7
Python 2.7.5 (default, Apr  2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

[xyz@innolx20122 ~]$ python3.6
Python 3.6.8 (default, Apr  2 2020, 13:34:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

It’s not working with python3.7 version

[xyz@innolx20122 ~]$ python3.7
Python 3.7.0 (default, Sep  3 2020, 09:25:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.7/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

Update-

We installed python following instruction in the below link
Python3.7 installation link

Hence my python3.7 version is installed at root level

[root@innolx20122 ~]# ls
anaconda-ks.cfg  Python-3.7.0  Python-3.7.0.tgz
[root@innolx20122 ~]# cd Python-3.7.0
[root@innolx20122 Python-3.7.0]# ls
aclocal.m4    config.status  Doc         Lib              Mac              Misc     PC              pyconfig.h     python-config     setup.py
build         config.sub     Grammar     libpython3.7m.a  Makefile         Modules  PCbuild         pyconfig.h.in  python-config.py  Tools
config.guess  configure      Include     LICENSE          Makefile.pre     Objects  Programs        python         python-gdb.py
config.log    configure.ac   install-sh  m4               Makefile.pre.in  Parser   pybuilddir.txt  Python         README.rst

I saw one link on stack overflow suggesting some workaround.
fix Sqlite3 issue

Kindly suggest if it’s ok to run below commands from same root directory itself

yum install sqlite-devel

./configure
make && make altinstall


Get this bounty!!!

#StackBounty: #linux #bash #centos #python3 #sqlite ModuleNotFoundError: No module named '_sqlite3'

Bounty: 50

We have different python version installed and specifically using python3.7 so I have edited my .bashrc file. We are using Centos7 with Linux server.

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
alias python=python3.7
alias pip=pip3.7


[xyz@innolx20122 ~]$ python
python             python2.7          python3.6          python3.7          python3.7m-config
python2            python3            python3.6m         python3.7m

[xyz@innolx20122 ~]$ which sqlite3
/usr/bin/sqlite3

Its working with python2.7 and python3.6 version

[xyz@innolx20122 ~]$ python2.7
Python 2.7.5 (default, Apr  2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

[xyz@innolx20122 ~]$ python3.6
Python 3.6.8 (default, Apr  2 2020, 13:34:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

It’s not working with python3.7 version

[xyz@innolx20122 ~]$ python3.7
Python 3.7.0 (default, Sep  3 2020, 09:25:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.7/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

Update-

We installed python following instruction in the below link
Python3.7 installation link

Hence my python3.7 version is installed at root level

[root@innolx20122 ~]# ls
anaconda-ks.cfg  Python-3.7.0  Python-3.7.0.tgz
[root@innolx20122 ~]# cd Python-3.7.0
[root@innolx20122 Python-3.7.0]# ls
aclocal.m4    config.status  Doc         Lib              Mac              Misc     PC              pyconfig.h     python-config     setup.py
build         config.sub     Grammar     libpython3.7m.a  Makefile         Modules  PCbuild         pyconfig.h.in  python-config.py  Tools
config.guess  configure      Include     LICENSE          Makefile.pre     Objects  Programs        python         python-gdb.py
config.log    configure.ac   install-sh  m4               Makefile.pre.in  Parser   pybuilddir.txt  Python         README.rst

I saw one link on stack overflow suggesting some workaround.
fix Sqlite3 issue

Kindly suggest if it’s ok to run below commands from same root directory itself

yum install sqlite-devel

./configure
make && make altinstall


Get this bounty!!!

#StackBounty: #linux #bash #centos #python3 #sqlite ModuleNotFoundError: No module named '_sqlite3'

Bounty: 50

We have different python version installed and specifically using python3.7 so I have edited my .bashrc file. We are using Centos7 with Linux server.

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
alias python=python3.7
alias pip=pip3.7


[xyz@innolx20122 ~]$ python
python             python2.7          python3.6          python3.7          python3.7m-config
python2            python3            python3.6m         python3.7m

[xyz@innolx20122 ~]$ which sqlite3
/usr/bin/sqlite3

Its working with python2.7 and python3.6 version

[xyz@innolx20122 ~]$ python2.7
Python 2.7.5 (default, Apr  2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

[xyz@innolx20122 ~]$ python3.6
Python 3.6.8 (default, Apr  2 2020, 13:34:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3

It’s not working with python3.7 version

[xyz@innolx20122 ~]$ python3.7
Python 3.7.0 (default, Sep  3 2020, 09:25:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.7/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

Update-

We installed python following instruction in the below link
Python3.7 installation link

Hence my python3.7 version is installed at root level

[root@innolx20122 ~]# ls
anaconda-ks.cfg  Python-3.7.0  Python-3.7.0.tgz
[root@innolx20122 ~]# cd Python-3.7.0
[root@innolx20122 Python-3.7.0]# ls
aclocal.m4    config.status  Doc         Lib              Mac              Misc     PC              pyconfig.h     python-config     setup.py
build         config.sub     Grammar     libpython3.7m.a  Makefile         Modules  PCbuild         pyconfig.h.in  python-config.py  Tools
config.guess  configure      Include     LICENSE          Makefile.pre     Objects  Programs        python         python-gdb.py
config.log    configure.ac   install-sh  m4               Makefile.pre.in  Parser   pybuilddir.txt  Python         README.rst

I saw one link on stack overflow suggesting some workaround.
fix Sqlite3 issue

Kindly suggest if it’s ok to run below commands from same root directory itself

yum install sqlite-devel

./configure
make && make altinstall


Get this bounty!!!

#StackBounty: #bash #redis #redis-sentinel Can't get redis-sentinel notification script to execute correctly

Bounty: 50

Installed latest version of redis/redis-sentinel (6.0.5) from Chris Lea PPA on Ububtu 20.04.

Created a notification script with the same path as suggested in the default conf.

Tested that the script can be executed as the redis user (using su).

Still getting script-error when I test a failover, etc.

No more ideas left, any suggestions?


Get this bounty!!!

#StackBounty: #bash #redis #redis-sentinel Can't get redis-sentinel notification script to execute correctly

Bounty: 50

Installed latest version of redis/redis-sentinel (6.0.5) from Chris Lea PPA on Ububtu 20.04.

Created a notification script with the same path as suggested in the default conf.

Tested that the script can be executed as the redis user (using su).

Still getting script-error when I test a failover, etc.

No more ideas left, any suggestions?


Get this bounty!!!

#StackBounty: #bash #redis #redis-sentinel Can't get redis-sentinel notification script to execute correctly

Bounty: 50

Installed latest version of redis/redis-sentinel (6.0.5) from Chris Lea PPA on Ububtu 20.04.

Created a notification script with the same path as suggested in the default conf.

Tested that the script can be executed as the redis user (using su).

Still getting script-error when I test a failover, etc.

No more ideas left, any suggestions?


Get this bounty!!!