1

Using "set -x" in bash script, outputs each executed command to an ouput file. eg here is part of the bash script

#!/usr/bin/env bash

# This is called by (my) .xinitrc, xdm and _should_ be called by gdm,
# kdm, xdm, lxdm whatever. However, gdm needs xorg-x11-xinit-session
# on fedora else 'ln -s .xsession .xsessionrc). Alternatively, create
# a Xsession login for gdm with /usr/share/xsessions/xsession.desktop:
#

# [Desktop Entry]
# Encoding=UTF-8
# Name=XSession
# Comment=This session will run your ~/.xsession
# Exec=$HOME/.xsession
# Icon=
# Type=Application
#
# [Window Manager]
# SessionManaged=true

ERRFILE="$HOME/.xsession-errors$DISPLAY"
exec &> $ERRFILE

echo "==============================================================="
echo "Running .xsession ... $(date)"
export PS4='+$(basename ${BASH_SOURCE}):${LINENO}:${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x

[[ "$WM" ]] || {
    F=~/.config/desktop
    [[ -f $F ]] && source $F
}
[[ "$WM" ]] || WM=$(type $WM 2>/dev/null)

# fallbacks:

[[ "$WM" ]] || {
    WM=$(
        type startfluxbox 2>/dev/null ||
            type awesome 2> /dev/null ||
            type startkde 2> /dev/null ||
            type gnome-session 2> /dev/null
      )
    [[ "$WM" ]] && {
        set -- $WM
        WM=$1
    }
}

[[ "$WM" ]] || {
    F=/etc/sysconfig/desktop
    [[ -f $F ]] && source $F
    [[ -x "$PREFERRED" ]] && WM=$PREFERRED
    WM=$(type $WM 2>/dev/null)
}

# final unction:
[[ "$WM" ]] || WM=myterm

[ "$DBUS_SESSION_BUS_ADDRESS" ] || {
    # eval `dbus-launch --sh-syntax --exit-with-session`
    echo ".xsession: sourcing /etc/X11/xinit/xinitrc.d/*"
    source /etc/X11/xinit/xinitrc.d/* # should do the above
}

echo ".xsession: .Xmodmap etc:"
MODMAP="$HOME/.Xmodmap"
MODMAPVNC="$HOME/.Xmodmap.vnc"
XRESOURCES=$HOME/.Xresources

Here is some sample bash script output courtesy of "set -x".

===============================================================
Running .xsession ... Fr 4. Dez 02:29:45 CET 2020
+.xsession:28:[[ -n '' ]]
+.xsession:29:F=/home/rgr/.config/desktop
+.xsession:30:[[ -f /home/rgr/.config/desktop ]]
+.xsession:30:source /home/rgr/.config/desktop
++desktop:1:WM=i3
+.xsession:32:[[ -n i3 ]]
+.xsession:36:[[ -n i3 ]]
+.xsession:49:[[ -n i3 ]]
+.xsession:57:[[ -n i3 ]]
+.xsession:59:'[' unix:abstract=/tmp/dbus-3769nUHNhO,guid=63a594a532295645f13a1a525fc99109 ']'
+.xsession:65:echo '.xsession: .Xmodmap etc:'
.xsession: .Xmodmap etc:
+.xsession:66:MODMAP=/home/rgr/.Xmodmap
+.xsession:67:MODMAPVNC=/home/rgr/.Xmodmap.vnc
+.xsession:68:XRESOURCES=/home/rgr/.Xresources
++.xsession:69:hostname -s
+.xsession:69:H=thinkpad-x270
+.xsession:70:XRESOURCES_HOST=/home/rgr/.Xresources.thinkpad-x270
+.xsession:71:XMODMAP_HOST=/home/rgr/.Xmodmap.thinkpad-x270
+.xsession:75:'[' -r /home/rgr/.Xresources ']'
+.xsession:75:xrdb -merge /home/rgr/.Xresources
+.xsession:76:'[' -r '' ']'
+.xsession:77:'[' '' -a -r /home/rgr/.Xmodmap.vnc ']'
+.xsession:78:'[' -z '' ']'
+.xsession:79:'[' -r /home/rgr/.Xresources.thinkpad-x270 ']'
+.xsession:80:'[' -r /home/rgr/.Xmodmap.thinkpad-x270 ']'
+.xsession:83:export BH_XSESSION=true
+.xsession:83:BH_XSESSION=true
+.xsession:86:xrandr -q
+.xsession:86:grep 'VGA1 connected'
+.xsession:92:ps -f -u bhepple
+.xsession:92:egrep 'gpg|ssh|gnome'
error: user name does not exist

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
+.xsession:94:echo '.xsession: print ssh & gpg settings (first):'
.xsession: print ssh & gpg settings (first):
+.xsession:95:env
+.xsession:95:egrep -i 'ssh|gpg'
+.xsession:95:sort
SSH_AUTH_SOCK=/run/user/1000/gnupg/S.gpg-agent.ssh
+.xsession:99:type gnome-keyring-daemon
gnome-keyring-daemon ist /bin/gnome-keyring-daemon
+.xsession:111:GPG=,gpg
+.xsession:112:type gpg-agent
gpg-agent ist /bin/gpg-agent
+.xsession:112:GPG=
+.xsession:114:GKD='gnome-keyring-daemon --start --components=secrets,ssh,pkcs11'
+.xsession:115:echo '.xsession: gnome-keyring-daemon --start --components=secrets,ssh,pkcs11'
.xsession: gnome-keyring-daemon --start --components=secrets,ssh,pkcs11
++.xsession:116:gnome-keyring-daemon --start --components=secrets,ssh,pkcs11
** Message: 02:29:46.108: couldn't access control socket: /run/user/1000/keyring/control: Datei oder Verzeichnis nicht gefunden
+.xsession:116:eval SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
++.xsession:116:SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
+.xsession:118:export GNOME_KEYRING_CONTROL GNOME_KEYRING_PID SSH_AUTH_SOCK GPG_AGENT_INFO
++.xsession:121:ps -ef
++.xsession:121:awk '/[g]nome-keyring-daemon/ {print $2}'
++.xsession:121:head -n 1

I had a look at compilation mode and the corresponding regexps but I havent a clue how to set the correct regexp for that output buffer. Is that even the way?

All I want to do is be able to use next-error in that output file in order to set the current line in the source script.

Any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.