27 February 2019

recreating a user

a short while back, i made an attempt to switch my home shell from bash to xonsh. in the process of doing this, i took more of a raw approach. despite the learning process that was, it left me with scars.

these scars are specific to my system's user management and my account (see the last paragraph of the shell switch post). i no longer had a recognized user requiring me to type in my username rather than just typing in my password.

today, i made the attempt to fix that. now, i will admit there might be a better way out there to do this, but i took this (raw) route again.

with superuser privileges, i edited the /etc/passwd file and appened a digit to my username and changed my user id to 1001. i did this in the /etc/group and /etc/shadow files.

i then created a new user with my original name using this command (as root):

# useradd mock -d /home/mock -M -u 1000 -G wheel -U

i set the password for the replacement user, and i rebooted the system for all the things to be recognized. (i came back later to add the other groups to which this user belonged.)

the command created the new user with the established name assigning the original user id to the new user and the original user's home directory also to the new user. because the user id is the same, all filesystem permissions still belong to the appropriate user.

i then removed the old, modified username from the /etc/passwd and /etc/group files.

now, things are back to normal.

08 February 2019

when to use less instead of vim

i am baffled that so many people feel like vim is the correct tool for viewing files on linux systems. using vim to view files you'd edit is fine. when i am viewing code or a config file which i may change, i might use vim but only if i plan on making changes to the file.

but when i am going to view a file which i don't ever plan to edit or ever need to edit, i use less. log files are ones where i see this happening very often. unless you expect to change the log file, i see no reason for using vim.

even when viewing files which could be edited, it's best to use less. scenario: someone wants to view a file. they use vim. i jump on the server and need to actually change the file. i get the message that the file is already being edited. maybe i can say that i want to edit it anyway, but there are no guarantees someone won't :wq or ZZ out of the file and undo my changes. if they used less, there would be no threat of this.

less lets me move through a file much quicker for pure examination purposes. spacebar works like CTRL+F to scroll a page at time, yet all the vim moving commands are available too. searching text is the same as vim. and i can exit less with a simple tapping of the q key.

"but, but i need line numbers and vim lets me do that!" so does less. use the -N option on less to get line numbers.

"what if learn that i need to edit a file after i have entered less?" just press v in less to enter vim mode.

and a side annoyance are any leftover swap files vim might leave behind on a dirty exit of vim. swap files for log files? how first-year cadet is that?

if you feel the outstanding need to use vim to view files which should never be edited, then use the -R argument to ensure the file is never changed. if you practice using the -R, i am willing to bet you'd find typing less much easier.

and before you ask me, "why not use more?" i will let you figure out why less is more. (and if you are viewing files with nano, there are no words to describe that insanity.)

bonus assignment: find out what the view command does.

25 October 2018

changing shells

i don't know if this a fedora-specific thing or a newer linux thing, but in the past changing your default shell was as simple as editing the /etc/passwd file. today, there is the chsh which can perform this for you. there is a catch, however. the shell must exist in the /etc/shells file to be allowed to be changed.

to make this work, i needed to add my shell to the /etc/shells file. then i issued the chsh to make my shell change.

$ chsh -l
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/usr/bin/tmux
/bin/tmux
/usr/bin/xonsh

$ sudo chsh -s /usr/bin/xonsh mock
Changing shell for mock.
Shell changed.

my previous post about xonsh is what led me here. i had manually changed the shell directly in the /etc/passwd file, but then my gnome session did not recognize my user. this was the strange side effect of changing the default shell not on the /etc/shells list. gnome (gdm) started the gnome-initial-setup process which runs when gnome is freshly installed.

and thing are still messed up. the gnome settings > users section shows no one, even though i am logged in under my account. (figuring out how to fix this will be another lesson for a future time.)

23 October 2018

xonsh

there was small thread on the knoxdev's slack account about shells the other day. as i was trying to continue the discussion cleverly using the shell names as regular words in sentences, i did a quick search for available shells from my package manager (i.e. dnf search shell). as i was looking through the results, i found xonsh.

the description listed in my package manager tells me this is what it is:
Description  : xonsh is a Python-ish, BASHwards-compatible shell language and
             : command prompt. The language is a superset of Python 3.4 with
             : additional shell primitives. xonsh (pronounced *conch*) is
             : meant for the daily use of experts and novices alike.
i have toyed around with the idea of using python's interactive shell as my main terminal shell for some time, but i always felt it would be too hard to do some of the simple things, such as file management. sure, i could write equivalent scripts to do those tasks, but i never made the effort.

this accomplishes exactly what i wanted to do. with xonsh, i can do this:

~ $ ls -l gPodder/
total 124
-rw-r--r--. 1 mock mock 90112 Feb 19  2018 Database
drwxrwxr-x. 3 mock mock  4096 Feb 12  2018 Downloads
drwxrwxr-x. 2 mock mock  4096 Feb 19  2018 Logs
-rw-rw-r--. 1 mock mock  2715 Feb 19  2018 Settings.json
-rw-r--r--. 1 mock mock 20480 Feb 19  2018 gpodder.net
~ $ from datetime import datetime
~ $ datetime.now()
datetime.datetime(2018, 10, 23, 8, 35, 50, 863981)

there is much more to xonsh than just this. the full documentation can be found at http://xon.sh.

09 April 2018

python string concatenation is a bad practice

i think because python is a scripting language, people tend to thing it's super fine to do something like this:

>>> val + '-string'

the problem is that the + operator does not do any casting of the variable to make it compatible with the string. this sort of thing will throw an error if the type of val is anything other than a string.

check it:

>>> val = None
>>> val + '-string'
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    val + '-string'
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
>>> '{}-string'.format(val)
'None-string'

>>> val = 4
>>> val + '-string'
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    val + '-string'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> '{}-string'.format(val)
'4-string'

now, you could force cast it using str(val), but that seems silly when you can just use the format() method on the string object to handle things for you.

02 March 2018

python is referential

this is often misunderstood about python, especially if you are accustomed to other languages, such as C/C++.

>>> class thing(object):
...     def __init__(self, some):
...         self.some = some
...     def add_on(self, addition):
...         self.some = self.some + addition
... 
>>> a = thing("hello")
>>> print(a.some)
hello

>>> b = a
>>> print(b)
<__main__.thing object at 0x7f9d267cf7f0>
>>> print(a)
<__main__.thing object at 0x7f9d267cf7f0>
>>> print(b.some)
hello

>>> a.add_on(", world")
>>> print(a.some)
hello, world
>>> print(b.some)
hello, world

>>> print(a)
<__main__.thing object at 0x7f9d267cf7f0>
>>> print(b)
<__main__.thing object at 0x7f9d267cf7f0>

28 November 2017

fedora magazine

last week, this interview with fedora magazine (online) was published. thanks to charles profitt for the write up.

https://fedoramagazine.org/mark-mcintyre-fedora/