Download file from Open Build Service (OBS) from command line

I really like the Open Build Service by the OpenSUSE team.

I wrote a tool that will download an OBS service run asset on the cli. This tool interacts with the authentication mechanism so you can get the relevant cookies and accomplish your task.

Check it out: https://gitlab.com/bgstack15/obs-dl

Yes, it’s insecure. Set environment variables USERNAME and PASSWORD and then run command:

./obs-dl https://build.opensuse.org/source/home:bgstack15/freefilesync/_service:recompress:tar_scm:FreeFileSync.tar.gz?rev=9410c094a8ce7fb9df4042c405ea0c7b

Save common commands on this server to .useful file

Command line productivity takes many forms. Some people live by bash autocompletion. I disdained it when I first started using bash (coming from ksh93) but over time realized that it’s a fantastic method for discovery (it’s much more complete on Debian-based distros than Fedora-based ones) and ease of use when typing long pathnames.

When you manage a disparate set of systems, and there are some tasks you repeat often, you usually try to list history and find the useful commands. One of mine:

sudo /var/storage1/shares/public/www/example.com/repo/devuan-deb/update-devuan-deb.sh

But over time, I have many commands I log in for, just to run the one command, and log out. So I have started using a text file: ~/.useful. Inside this file, place your oneliners.

So whenever you log onto a box, and you need to see what commands you run here commonly, check what you’ve saved:

cat ~/.useful

Of course, it’s only useful if you bother to populate it.

echo 'vi $( newest /var/log/debmirror/ log )' >> ~/.useful

Notes for git log

git log --graph --oneline --all
* 5240127 cladu.sh: fix the tluid=tduid check syntax
* 9a4afe3 fix comments about branch in metadata of various files
*   93893b0 Merge remote-tracking branch 'origin/master' into work1
|\  
* \   7ba8d7e Merge branch 'master' of https://gitlab.com/bgstack15/bgscripts into work1
|\ \  
* | | 476f863 add cladu and bump to 1.3-4
* | | abbb687 userinfo: add chage
* | | 3c87f21 fix work1 branch again
* | | b9caef9 update %files core in spec

Git log output

Convert Local to AD Users

Project CLADU

CLADU stands for Convert Local to AD User.

When you want to take local accounts and remove them and have the AD user with the exact same name take its place, use cladu.

usage: cladu.sh [-duV] [-gr] [--ng] [--nr] user1 [ user2 user3 ... ]
version 2018-03-09a
-d debug Show debugging info, including parsed variables.
-u usage Show this usage block.
-V version Show script version number.
-g groups Add the AD user to the local groups of the local user. Default is to skip this action.
--ng Do not perform the -g action
-r report Generate report in each user homedir.
--nr Do not perform the -r action
Environment variables:
Parameters override environment variables
CLADU_USERINFO_SCRIPT=/usr/share/bgscripts/work/userinfo.sh
CLADU_USER_REPORT any non-null value will perform the -r action.
CLADU_USER_REPORT_FILENAME=converted.txt File to save report to in each homedir
CLADU_GROUPS any non-null value will perform the -g action.
Return values:
0 Normal
1 Help or version info displayed
2 Count or type of flaglessvals is incorrect
3 Incorrect OS type
4 Unable to find dependency
5 Not run as root or sudo

Go check out the entire source to look at the flow of the script.

xfconf-query save and load from file

xfconf-query load from file

Introduction

The wonderful xfce desktop environment provides a mechanism to inspect and modify your settings, similar to dconf. This tool is named xfconf-query, and it allows you to list and modify entries one at a time. Unfortunately, it does not provide a way to export to a file and import, the way dconf does (with standard redirection).

Save settings to file
For xfce, you can display the settings by specifying the channel:

xfconf-query -c thunar -lv

#place sample output here

You can save this to a file with output redirection, but you won’t be able to load this very easily from such a file.

To get the settings in a nicer format for saving to a text file, use this oneliner:

xfconf-query -l | sed -r -e '/Channels:/d' | while read line; do xfconf-query -lv -c "${line}" | sed -r -e "s/^/${line} /"; done > my-settings.xfconf

#place sample output here

xfconf-query load settings from file

I wrote a wrapper script that loads the settings from such a file. Please check out the full xfconf.sh script at gitlab.
Its basic use is very simple. Call the script with the settings file as the only parameter:

xfconf.sh mysettings.xfconf

Code walkthrough

#!/bin/sh
# File: /usr/share/bgconf/inc/xfconf.sh
# Author: bgstack15
# Startdate: 2017-09-17 08:10
# Title: Script that Loads Settings into Xfconf
# Purpose: To make a single interface for other bgconf scripts to call for loading an xfconf file
# History:
#    2017-06 Main research was done but put in separate bgconf scripts.
#    2017-09-17 I decided to separate it out to streamline the bgconf scripts themselves.
# Usage:
#    In a script, determine that an xfconf file exists, then call:
#       xfconf.sh mysettings.xfconf
#    To generate a new xfconf file, you can run:
#       xfconf-query -l | sed -r -e '/Channels:/d' | while read line; do xfconf-query -lv -c "${line}" | sed -r -e "s/^/${line} /"; done > outfile
# Reference:
# Improve:
# Document: Below this line

thisDE=xfce
thisDEconf=xfconf-query
infile="${1}"

# get DBUS_SESSION_BUS_ADDRESS of first DE process of this user
# reference:  https://unix.stackexchange.com/questions/29128/how-to-read-environment-variables-of-a-process/29132#29132
tmpfile1="$( mktemp )"
if test -n "${SUDO_USER}"; then _user="${SUDO_USER}"; else _user="${USER}"; fi
cat /proc/$( ps -eu${_user} | grep -E "${thisDE}" | tail -n1 | awk '{print $1}' )/environ 2>/dev/null | tr '\0' '\n' | grep -E "DBUS_SESSION_BUS_ADDRESS|DISPLAY" > "${tmpfile1}"
test -f "${tmpfile1}" && test $( grep -cE "(DBUS_SESSION_BUS_ADDRESS|DISPLAY)=.+" "${tmpfile1}" 2>/dev/null ) -ge 2 || echo "$0 error: Skipping ${thisDE}: Could not find current session." 1>&2
chmod +rx "${tmpfile1}" 2>/dev/null
. "${tmpfile1}"
/bin/rm -f "${tmpfile1}" 1>/dev/null 2>&1

# Assume infile exists as a file
if test -n "$( cat "${infile}" 2>/dev/null )" && test -x "$( which "${thisDEconf}" )" && ps -ef | grep -qE "${thisDE}" && test -n "${DBUS_SESSION_BUS_ADDRESS}";
then

   # get user of that directory
   thisowner="$( stat -c '%U' "${infile}" )"
   thisowneruid="$( stat -c '%u' "${infile}" )"

   # xfce custom configuration
   grep -viE '^\s*((#|;).*)?$' "${infile}" | while read channel attrib value;
   do

      # display output
      #printf "channel=%s\tattrib=%s\tvalue\%s\n" "${channel}" "${attrib}" "${value}"

      # provide data type. This needs to be researched before making a new .xfconf file.
      _thistype=string
      case "${attrib}" in
         *last-separator-position) _thistype=integer ;;
         *last-show-hidden|*misc-single-click) _thistype=bool ;;
      esac

      # make change
      sudo su - "${thisowner}" -c "DISPLAY=${DISPLAY} DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS} ${thisDEconf} --create -t ${_thistype} -c ${channel} -p ${attrib} -s ${value}"

   done

fi
/bin/rm -f "${tmpfile1}" 2>/dev/null

The line numbers here are different from the script on github, and probably will get outdated as I make improvements to this utility. So I will use line numbers for the version seen above. Also, this script was originally written as a small portion of a larger project to deploy my settings to a whole system. That’s why you’ll see the “sudo – su” and logic to determine file ownership, because it is being called by a command running with sudo.

Lines 23-31 find the running desktop environment for the user (or the user who called sudo) and grab the values for DBUS_SESSION_BUS_ADDRESS and DISPLAY that point to that running desktop environment. I don’t know a more official way, so I assembled this kludge over the course of this project. I’m rather fond of this logic despite the kludgeyness. You will observe on line 30 that this script actually dot sources a temp file with those variables. I actually first used this technique for loading conf files in an attempt to be more unix-like and use environment variables first, and then load in a conf file.

Line 34 calculates the requested file has contents, and the desktop environment really exists and is running, and one of the variables from the previous section is defined.
Lines 42-58 perform the actual import of settings from the file. The interesting regular expression is my official non-blank non-comment regex. For quick hand-typed oneliners, I normally just use ‘^$|^#’ but here I went with the fancier version that handles whitespace.
So the block reads three entries per line, the channel, item, and item value. Then it runs xfconf-query and plugs in the variable.
Lines 48-53 perform a manual type declaration based on hard-coded names. I don’t know how to query that from xfconf-query, so I had to use the graphical xfce settings tool to collect the exact names. You will definitely need to read through your xfconf files to make sure you include all the right options here. I suppose one could find a list of all the datatypes maybe from xfce’s documentation and parse it. I guess I’ll add that to the “Improve:” header.

dconf save and load from file

dconf save and load to file

GNOME-based desktops use a settings utility that is a little similar to the registry of a famous non-free operating system. I’ll spare you the ideological diatribe and get to the task at hand. I use Cinnamon from the Linux Mint project, and it is based on GNOME 3.

The command line tool for manipulating the settings is titled dconf.

Saving dconf settings to file

Dumping its output is easy.

dconf dump /
[net/launchpad/plank/docks/dock1]
icon-size=32
show-dock-item=false
position='left'
dock-items=['org.gnome.Terminal.dockitem', 'nemo.dockitem', 'firefox.dockitem']
unhide-delay=0
items-alignment='center'

Redirect to a file and you’re done.

dconf dump / > my-cinnamon.dconf

Pick a subdirectory if you wish to narrow it down.

dconf dump /org/cinnamon/sounds/
[/]
maximize-enabled=false
unmaximize-enabled=false
tile-enabled=false
map-enabled=false
close-enabled=false
minimize-enabled=false
switch-enabled=false

Loading dconf settings from file

The reverse is also as easy.
Make sure you use the same directory in the layout.

dconf load / < my-cinnamon.dconf

The story

This post is a precursor to a discussion about manipulating the settings programmatically in xfconf-query, which is the settings cli tool for the xfce desktop environment.
I wrote a wrapper script for a project of mine. Check out dconf.sh at gitlab underneath my project bgconf.

Prepend output with time to generate each line

To show how long it takes before showing each new line of output, use this neat command.

long_command | ts -i "%.s"
$ ./configure --prefix=/tools | ts -i %.s
0.082337 checking for a BSD-compatible install... /tools/bin/install -c
0.002841 checking whether build environment is sane... yes
0.008164 checking for a thread-safe mkdir -p... /tools/bin/mkdir -p
0.000040 checking for gawk... gawk
0.005892 checking whether make sets $(MAKE)... yes

References

Weblinks

  1. Stéphane Chazelas at https://unix.stackexchange.com/questions/391210/prepending-each-line-with-how-long-it-took-to-generate-it/391222#391222

Restart cinnamon from command line

When cinnamon freezes up and needs to be restarted, you can restart it from Cinnamon itself or from a different terminal.

In Cinnamon

Press ALT+F2. Type in the letter r and press enter.

On the command line

To switch to another console terminal, press CTRL+ALT+F2.
On this terminal, type this command.

pkill -HUP -f "cinnamon --replace"

References

Weblinks

  1. User sim at askubuntu.com https://askubuntu.com/questions/143838/how-do-i-restart-cinnamon-from-the-tty/523436#523436

Save settings for bc, the Linux calculator

tl;dr

Use a ~/.bcrc file with contents:

scale=4

Execute this or put it in your bashrc:

export BC_ENV_ARGS=~/.bcrc

Explanation

In GNU/Linux, a basic calculator is bc from package bc.
The normal output is displayed with zero values after the decimal point, so I like to adjust it to show me four numerals past the decimal point.

scale=4

You can use whatever scale you like.
To get this to take effect every time you call bc, you can set an environment variable BC_ENV_ARGS to a filename. That file is parsed as regular instructions to bc, before it enters the interactive shell or interprets standard in.
So modify a file, such as ~/.bcrc with your instructions.
Update your shell (or add to your .bashrc file):

test -f ~/.bcrc && export BC_ENV_ARGS=~/.bcrc

References

Weblinks

  1. Inspiration for this post http://linux.byexamples.com/archives/42/command-line-calculator-bc/
  2. Description of “rc” files in Unix https://en.wikipedia.org/wiki/Run_commands

man pages

bc