Posts

Showing posts with the label bash

Running arbitrary containers in LinuxKit

Debugging issues with LinuxKit images could be a serious challenge in some cases. Unfortunately, the documentation is not full enough to cover all the caveats, and in this article I'm going to show the general principle how to deal with arbitrary containers and run the commands in them.

Emulating line-buffering mode (Gradle example)

C printf() function doesn't flush buffers by default, and a lot of code using it may miss this, therefore creating interactivity problems to the end user. It's not specific to C though - it's about the standard I/O streams, and the problem may appear anywhere. The post is based on StackExchange discussion, so you can dig deeper if you want:  Turn off buffering in pipe .

shelljs node module

Let me share a little gem I've found: shelljs node module . I was working on updating my Cordova hooks to work with Windows environment and had to replace my bash script with the corresponding JS code. Unfortunately, the node modules I've found were disappointing, especially copying the files. Surely I could write the copying code by myself, but I supposed that in 2016 year one is not required to do such menial tasks. The biggest problem for the modules I've tried was the glob matching - I have to copy files from the sibling directory, and modules couldn't find it (looks like they don't parse ".." notation). I've tried fs.extra, fs-extra, ncp, filecopy, cp, but no luck. I guess I could find the good module after all, but after I've discovered shelljs I don't need it anymore anyway. With shelljs porting the code was a piece of cake - see the results below (the original and the new scripts).

Mini HOWTO: Getting file names in Zip-archives using Bash

Image
I'm gathering stats about my archives, and one of these is getting all the file names in them. There are some challenges about it, so let me show the required commands. Getting file names from the one archive: unzip -l /path/to/zip-file | tail -n +4 | head -n -2 | cut -c31- Executing pipelined commands in xargs: xargs -I {} -i sh -c 'command1 | command2 | ... | commandN' For my case I've used the expression: find . -iname "*.zip" -print0 | xargs -0 -n1 -I {} -i sh -c 'unzip -l {} | tail -n +4 | head -n -2 | cut -c31-' | sort | uniq -c Yeah, yeah, black magic, gotcha Good luck!

Daemonize a script

Sometimes it is required to start script as daemon (for example Django site in development mode), and I want to provide guidance how to do it in Fedora 9. First, it is required to write auxiliary bash-script for running necessary script (let's call it 'site.sh'): #!/bin/sh cd /path/to/site/ nohup python manage.py runserver 0.0.0.0:8080 --noreload > site.log & echo "${!}" > /var/run/site.pid In this script I changed directory to site location, and ran it via 'nohup' command. Also I took PID of created process via '${!}' to manage it later. This script should be run under root privileges and should be checked via 'ps aux | grep python' for equality of PID of running process and stored in /var/run/site.pid. If everything is fine, let's move forward and create init-script (let's call it 'site'): #! /bin/sh # Startup script for site # # chkconfig: 2 96 04 # description: site service # Source function library. ....