Bash
SSH/SCP quick help
Challenge was to reconfigure a device, which has a Linux running on a TQ-board. Some files had to be adjusted.
Connect to the device:
1 |
ssh <usernam>:<ip> |
(Then enter password.)
List amount of free space:
1 |
df -K . |
Show target of symlink:
1 |
readlink <file> |
how to resume broken downloads
aka: fix the weird firewalling settings ..
aka: How to download the latest stable Qt SDK source code?
curl -L -O -C - http://download.qt.io/official_releases/qt/5.12/5.12.3/single/qt-everywhere-src-5.12.3.zip
cmd replacement: cmder
Sometimes you have to call some batch-files with spoecific windows-tools, so a bash is not helpful on Win.
A colleague recommended cmdr, which is quite nice and fine.
Suits my needs 🙂
advanced whitespace-correction
(for CMake/C++-projects)
Find all fitting files and run the fixer-script in parallel over it.
1 |
( find . -name "CMakeLists.txt" ; find . -name "*.cmake" ; find . -name "*.h" ; find . -name "*.cpp" ) | parallel --bar ../removeTrailing.sh |
After playing for a while with sed and awk and not being able to get a fitting solution, I decided to create my own as python-script. It squashes all consecutive double-whitespace-lines (and adds one to the end if missing).
Or “run it on the files changed for the last commit”:
1 |
$ git diff --name-only HEAD~1 HEAD | parallel --bar ../removeTrailing.sh |
Sources:
removeTrainling.sh
squashMultipleWhitespace.py
How to get GNU parallel (developed by Ole Tange):
1 |
(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash |
Pick a random reviewer
Most teams have a certain code-review-process. But most of the time there is no “round robin”-implementation who will be the reviewer, but the coder itself should select the reviewer. Because of domain-knowledge, etc. And then you her lots of times “I am currently busy. Not me!”
Solution: random and fair selection via bash.
1 2 3 4 5 6 7 8 |
#!/bin/bash # initialize the array array=(MSA MBH HGA GSC MPE NLE RNI MLA) size=${#array[@]} index=$(($RANDOM % $size)) echo "Mister ${array[$index]} is your candidate as reviewer. Congratulations!" echo "In case you picked yourself - just rerun ;))" |
Current version of the script can be found at: https://github.com/marcelpetrick/bashCollection/blob/master/randomReviewer.sh
Inspired by a stack-overflow answer.
find the fattest files of certain type
1 |
$ find . -name "*.LSimg" -exec ls -al {} \; | sort -nr -k5 >> listOfFattestFiles.txt |
creates something like
1 2 3 4 |
-rw-r--r-- 1 mpe 1049089 323707001 Mai 29 2018 ./a.LSimg -rw-r--r-- 1 mpe 1049089 21342433 Mai 29 2018 ./b/c.LSimg -rw-r--r-- 1 mpe 1049089 21342432 Mai 29 2018 ./e.LSimg .. |
Fix whitespace
Sometimes there is too much whitespace and tabs in my last commit.
In former times I used a VisualStudio-plugin, but this is not helpful there. So I wrote a small script (see on my github) which:
• replaces all tabs with four spaces
• removes all trailing whitespace
• converts line-endings to CRLF
I invoke it on the list of all changes files in my last commit in the git-repo with:
1 |
$ git diff --name-only HEAD~1 HEAD | xargs -L1 ../removeTrailing.sh |
(first part gives you a list of changed files and then feeds it to the script for execution)
Overview of author-activities for the git-repo
If some ‘sophisiticated’ tool like git-stats is too cumbersome to configure, just do some git-log and shell-magic:
1 2 3 4 5 6 7 |
$ git log --format='%aN' | sort | uniq -c | sort 4 Hans Wurst 6 Klaus Blume 6 Bernd Meier 26 Marcel Petrick 32 Ali Baba 37 Linus Torvalds |
Ok, just noticed that git has something built-in:
1 |
$ git shortlog -sn |
from: https://coderwall.com/p/pek-yg/git-statistics-for-repo-per-author – Thanks Marcin Olichwirowicz!
keep the catcam up to date
Logging in to the raspberry(ies) to update them on a regular basis takes time and effort. Both are currently dear.
Also: while the catcam is taking pictures, you can’t update the rpi-binaries.
So I made this nice script, which first suspends the catcam-operation while renaming the script from the cronjob, then does all updates, reverts the renaming and then reboots the MCU.
1 2 3 4 5 6 7 8 9 10 11 |
#!/usr/bin/env bash # author: Marcel Petrick (mail@marcelpetrick.it) mv /home/pi/Desktop/webcam/regulargif.sh /home/pi/Desktop/webcam/regulargif.sh1 sudo rpi-update sudo apt-fast update && sudo apt-fast dist-upgrade -y sudo apt-get autoclean sudo apt-get autoremove mv /home/pi/Desktop/webcam/regulargif.sh1 /home/pi/Desktop/webcam/regulargif.sh sudo reboot |
Make it executeable via ‘sudo chmod +X updateCatcam.sh’ and also add it as cronjob (once a day).
rsync: get all files from the camera’s sd-card .. without interruption
1 |
rsync -avzr --progress /media/tanteedith/4C26-6BEF/DCIM/ /home/tanteedith/digikamArchive/ |
Of course, you need the same source- and target-directory 😉
Using this, because Nautilus (or how that horrible explorer is called) is prone to auto-unmounting the card while transfer due to heavy load ..