26Jul/18

bash convert number to have thousands seperator – Linux & FreeBSD

Convert a number like 10001 to 10,001

FreeBSD

or

LINUX

From this article: https://unix.stackexchange.com/questions/113795/add-thousands-separator-in-a-number

example:

The end

27Apr/18

Find UPNP devices on your network – ex: ReadyDLNA or UPnP routers

Got info from here: https://stackoverflow.com/questions/18363833/how-can-i-list-upnp-server-renderer-in-command-line-console-mode-on-linux

Get the Upnp test tools

Example 1:

Find all Upnp devices (note their target names start with urn:…. It will hint at what service it is, ex: Layer3Forwarder, MediaServer, etc..)

Example 2:

Find DLNA servers which are served via Upnp (which has a target of MediaServer, urn:schemas-upnp-org:device:MediaServer:1)

The end

24Jan/18

bash simple variable substitution

You can use bash to do simple substitutions of variables, just like you can with sed. The bash trick turns out to take up less characters (bytes).

Lets set some variable

To replay it back:

or

Output of both:

 

Format

To do a simple substitution of the value/contents of a variable structure it like so. Put in the from part the thing you want to change (it can be a char or some chars), then it will replace them with the part you put in to (which can be a char or several chars). Note if mentioning special chars to escape them (example \ needs to be \\). Spaces are not considered special chars in this case.

echo ${VAR//from/to}

Then all of the parts that are from will change to to when the variable VAR is called.

Example 1

Lets replace all s with a S. You have to use the second notation type and add some parameters

Output:

  • Note: sed can be used to do the same substitutions

or

 

Example 2

Let replace all is with IZ

Output:

  • Note: sed can be used to do the same substitutions

or

 

Example 3 – filenames with spaces

What about converting an absolute path which has spaces to something with escaped spaces (backslash followed by space)

Output:

We convert spaces ” ” to “\ ” however in bash to print a backslash “\” we need to put an extra one “\\”

Output:

  • Note: sed can be used to do the same substitutions

or

The end.

15Nov/17

Bash scripting dealing with any characters in strings or filenames

Imagine a file list, list.txt, like this one

Naturally you could deal with it like so

Or just use the $i variable as its already the file and do a one liner like this:

cat list.txt | while read i; do dosomething "$i"; done This will dosomething against image1.jpg, image2.jpg and image3.jpg.

Easy simple loop.
Now imagine a file list with special characters like this one, listB.txt:

The same loops will not work and will error out. So you have to treat them like so:

Note: do not put quotes around the eval.

This will then properly process the files as it will naturally escape them.
It will properly run dosomething against those 5 files.

How does it do that? You can print out the FILE variable to understand. It simply backslashes (escapes) all of the special characters.

Output:

Sidenote: from talking with others, nohup dosomething &> whatever &  can be weird if dosomething has to operate on file with special characters. Instead its better to put it in the background and then just disown it, like so:

 

The end.

26May/17

My Favorite X-Plane 10 / 11 Controls – Rudder, Joystick & Throttle Setup

I do not take credit for this controller layout. I really like it and would like to have a copy easily available so I hosted it here, also I would like to share with you all my favorite controllers. These have been designed by Catstrator, all credit goes to him (if you see this, I hope its okay that I am using this). Full credit goes to you good sir!

Xplane Controls (credit goes to catstrator) – click on picture to enlarge

Joystick & Throttle: Saitek Pro Flight X-55 Rhino H.O.T.A.S. (Hands on Throttle and Stick) System for PC

Rudder: Saitek Pro Flight Rudder USB Pedals (these are different than the picture, however they both support Yaw & Left & Right Toe Break)

Setup Video for X-Plane 11 (there is also one for X-Plane 10 on his channel): https://www.youtube.com/watch?v=hFn-zztS5ho

 

26May/17

Repeat Command Until Success – recursive method – rrsync, repeat, repeatrecurse

Here I show you how to repeat a command until it finishes successfully. This should only be done with programs that support resume and also have legit exit codes (or else they might run on for forever – read on). I show how I constructed such a function, by first showing how to create a function that repeats rsync only. Then we build a function that can repeat anything.

Sometimes you will run a command and it will fail. However you can repeat that same command to resume it where it left off. A good example is “rsync”. rsync can be made to resume from where it left off – I use the partial option (-P gives progress bar and partial option at the same time). This can be done with any command that supports resume and has good exit codes (meaning success is 0, and fails are none 0).

Two criteria for this to work

  1. supports resume in a good way (cp supports resume, but its not good – see below, rsync supports good resume)
  2. has good exit codes (0 is success, none-zero for none-success)

Note that “cp” is not a good example of a command that can resume where it left off (even if you use -u), because it will skip the files that where partially copied. So if you have 10 big files, and the “cp” got cut off mid 5th file, when you repeat the command, it will just continue with the 6th file. The 5th file will be incomplete.

We will use a recursive method to do this repeating resume operation.

RRSYNC – Repeating Rsync

First I will show how to do this with rsync. We basically run rsync, then if its exit code is zero we exit out, if its exit code is none zero we repeat the function.

Check the comments for how to run it. Basically you would first copy paste that function into a shell and then run this  rrsync 1 -avhP <src> <dest> . Also if you had the above code in a script file. Such as rrsync.sh. You would need to source the script first to get that function to be available in the shell source rrsync.sh  or the shorthand way . rrsync.sh

Note I decided to keep track of a variable (so that I can show which run number this is), and the only way to do that within a recursive function is with global variables, or a variable that you pass on to the next level. Since I didnt want to use global variables, I just passed it on to the next level, the caveat is that we have to specify the start value of this variable when we begin (hence the 1 before the arguments)

REPEAT function

The next code allows me to repeat any function. I didnt want the user to worry about having to provide 1 all the time, so I created a starting function called “repeat”, which calls the code that does everything “repeatrecurse”.

So copy&  paste the above two functions into a shell to source them (or source them thru a script with the source  bash builtin), so that you can use repeat. Then to utilize the repeat function do this:

Rsync will then repeat until success.

You can test the code by creating two small functions one which always returns success (exit code 0, so it will only run once), and one that will always return none-success (none-zero exit code, so it will run forever until someone presses Control-C or kills it)

You can test them like so

Here we show how it runs once when we have a successful end

Here we show how it repeats

The end

 

21Oct/16

FreeBSD – Show info about running process – SIGINFO

When you are running a command hit Control-T anytime, its very useful when a command looks stuck.

It causes the kernel to spit out a line similar to the following:
load: 0.69 cmd: awk 56538 [piperd] 0.96r 0.00u 0.00s 0% 2380k

Shows load 1m load-average (“load: 0.69”), the ucomm (utility basename) of the running process (“awk”), the process ID (PID) of the running process (56538), the wait channel or wchan status (“piperd” for pipe read), the real number of seconds the command has been running thus far (“0.96r”), the user seconds (“0.00u”), system seconds (“0.00s”), CPU utilization (0%) and rss or resident set memory size (“2380k”)

That line comes from the kernel and not a sub-process, userland process, nor forked process

When you get more than just the “load: …” line out of a Ctrl-T, for example… “sleep: about 99 second(s) left out of the original 100”

The extra information comes from the running process itself responding to a SIGINFO that was generated by the kernel and sent to said running process (following the initial strike of Ctrl-T and printing of “load: …” line)

Ctrl-T does not and cannot interfere with a running program

27Sep/16

Enable Mouse Scrolling with Screen (tested in Putty)

Screen the app that can split screens like so, is not too intuitive when it comes to scrolling thru your bash shell. Notice where the cursor is in the diagram below, its in the top right window, sitting at an empty prompt right after an ls -1 was run. Lets say I wanted to scroll up. I cant use the mouse wheel (yet), however I can use Control-A [ and then use the arrow keys to scroll up and down (pressing q when im done)

By default mouse wheel support is not enabled.

To enable mouse wheel support in screen you will need to add the following lines to your screen configuration file ~/.screenrc. I would also recommend to make sure you have the latest version of screen installed (I am using 4.04).

I just put the following two lines in my .screenrc in my $HOME dir (thats all thats in my .screenrc)

I tested and this and it works with Putty. This should also work in Max OSX and also with Cygwin.

Sidenote: I might actually be misleading you and it might not work so well when you have screen doing window splits. However it works well when the windows are not split. Thats for you to test and find out.

Some other recommendations, that I have seen, are to launch screen like so:

Sidenote: If you are already in a screen session when you make these changes, you will not see them be activated, first you need to detach, and then reattach to your screen session. Detach screen like so: Control-A followed by little d  (a.k.a. Control-A+d, or simply ^A d). Then create the file ~/.screenrc (if doesnt exist, and if it does, just add those lines). Finally reattach to your screen detach with screen -x (which will reattach to your last session). If you have several detached screen sessions then you can use screen -ls to see all of your screen session listings. You can connect to different screen sessions by their ID screen -x ID (example: screen -x 9532). Where ID, you get from screen -ls.

Sidenote: try the scroll wheel in a new screen session. If your having troubles not getting mouse wheel support after editing the file, maybe kill all of your screen sessions and try it.

The end.

20Sep/16

Show and Print Tabs in Linux and Freebsd

To print tab characters (and other control characters) you cant just press Tab because tab is a special auto complete char in bash.

Control-V then Tab or Control-V then Control-I (also known as ^I) – Tab character – so to print a Tab character you can use both of these combos

Example:

echo “Here comes a tabControl-V then Tabwhatever”

Will show: as echo “Here comes a tab      whatever”

Hitting enter you will see

Here comes a tab     whatever

Sidenote: In this article I actually used spaces to represent a tab (whatever, whocares)

Sidenote: if you select the tab character from the shell (by dragging a box around the echo or around the output) and paste it within a putty again it will actually put spaces into clipboard, but using bash history with up and down arrows you will actually have the tabs preserved. So with clipboard the tabs turn to spaces, but in bash history its actually shown as Tabs.

So to the outside viewer a tab or a few spaces look similar, so how do you distinguish them? One way is to add some characters and see how tab jumps things around more than a few character spaces (where as space always just keeps the same distance)

The best way, in my opinion, is to pipe the output of whatever text into cat -tevu (the t specially will convert all Tabs into ^I)

echo “Here comes a tabControl-V then Tabwhatever” | cat -tevu  (the bold are special keys to press)

Here comes a tab^Iwhatever

The ^I means tab, a single tab char. If I saw ^I^I that means there were two tabs (Control-V then Tab, followed by Control-V then Tab again, will get us two tabs on the shell)

sidenote: there are other control characters, not just tab, for example ^X (control-V control-x) is the delete character it will show up as ^X but it will act as a delete in the output. cat -v shows those.