Skip to content

Instantly share code, notes, and snippets.

View zipizap's full-sized avatar

zipizap

View GitHub Profile
@zipizap
zipizap / compress_video.sh
Created August 17, 2014 09:01
compress a video using avconv
#!/bin/bash
[[ -r $1 ]] || { echo "Usage: $0 <videofile-to-be-resized> [<videofile2> ...]"; exit 1; }
for INPUT_FILE in "$@"; do
OUTPUT_MP4_FILE=$(echo "$INPUT_FILE" | sed 's/\.[^.]*$/_resized.mp4/g')
#See https://trac.ffmpeg.org/wiki/x264EncodingGuide
avconv -y -i "$INPUT_FILE" -threads auto -c:v libx264 -preset medium -b:v 988k -pass 1 -an -f mp4 /dev/null && \
avconv -i "$INPUT_FILE" -threads auto -c:v libx264 -preset medium -b:v 988k -pass 2 -c:a libmp3lame -b:a 192k "$OUTPUT_MP4_FILE"
rm -f av2pass* &>/dev/null
@zipizap
zipizap / parse_multiple_args_in_bash_example.sh
Last active August 29, 2015 14:04
Example of how to parse mutliple arguments in bash
#!/bin/bash
[[ $# -gt 1 ]] || { echo "Usage: $0 <arg1> <arg2> ... <argN>"; exit 255; }
echo 'Dont forget to surround the "$@" with the double-quotes '' so that it does not divide arguments that contain spaces.'
echo 'Samewise, you also need to surround the "$ARG_I" var'
for ARG_I in "$@"; do
echo '$@ = '"$@"
echo '$ARG_I = '"$ARG_I"
done
@zipizap
zipizap / run_as_user.sh
Last active August 29, 2015 13:56
bash function to run command as another user
function run_as_user {
local NEW_USER=$1
local CMDS=$2
sudo -u $NEW_USER -i bash -l -c "$CMDS"
}
#Use it like
run_as_user luke "echo $PWD; id"
@zipizap
zipizap / bash_local_variables
Created February 11, 2014 13:04
bash - global variables vs local variables
#!/usr/bin/env bash
# In bash all variables are defined by default as GLOBAL, even if defined inside functions
# To define a variable as LOCAL to a function, we have to prepend "local" in the definition
# This makes the variable only defined in the current function scope and so not global.
VAR_GLOBAL="im global - you can read and change me from anywhere, even inside functions - which may not always be a good thing"
echo "Seen from outside: $VAR_GLOBAL"
function my_func {
@zipizap
zipizap / gist:7947933
Last active December 31, 2015 06:29
Understanding better strace, in order to write up a good reply to C Evans comment :)
#
# This gist is related to C Evan's comment on
# http://zipizap.wordpress.com/2012/10/15/xkl-ubuntu-install-resizing-ntfs-partition-takes-too-long-check-ntfsresize-from-console-with-strace/
#
# Hi Evans :)
#
# I see what you mean: only strace the writes to stdout, and not all writes that
# also contain moved data of the resize... And it got me interested in better understanding
# the strace options, so I did a little "man strace" study and then a little program in C
red@ownc:/tmp$ cat a.sh
#!/bin/bash
# Activate debug
set -x
echo "hi"
ls
pwd
@zipizap
zipizap / du_sort.sh
Created December 10, 2013 10:04
Solaris "du" does not have the option "--max-depth=1", and this simple script eases a little bit of that pain... Output is shown in Kb and sorted by size
du -k . | egrep -v '\/.*\/' | sort -n
#!/bin/bash
[[ -r $1 ]] || { echo "Usage: $0 <videofile-to-be-resized> [<videofile2> ...]"; exit 1; }
for INPUT_FILE in $@; do
OUTPUT_MP4_FILE=$(echo $INPUT_FILE | sed 's/\.[^.]*$/.mp4/g')
#See https://trac.ffmpeg.org/wiki/x264EncodingGuide
avconv -y -i $INPUT_FILE -threads auto -c:v libx264 -preset medium -b:v 988k -pass 1 -an -f mp4 /dev/null && \
avconv -i $INPUT_FILE -threads auto -c:v libx264 -preset medium -b:v 988k -pass 2 -c:a libmp3lame -b:a 192k $OUTPUT_MP4_FILE
rm -f av2pass* &>/dev/null
@zipizap
zipizap / check_files_integrity.sh
Created December 4, 2013 15:25
Check that all files under current dir are readable. I use it to check the decrypted-version of files ofan EncFs dir... to check that the decrypted files are all ok... if the EncFs dir is in Dropbox and the upload/sync fails, then the decrypted-version will have errors, which can be spoted with this simple script
#!/bin/bash
function shw_grey { echo -e '\033[1;30m'"$1"'\033[0m'; }
function shw_norm { echo "$1"; }
function shw_info { echo -e '\033[1;34m'"$1"'\033[0m'; }
function shw_warn { echo -e '\033[1;33m'"$1"'\033[0m'; }
function shw_err { echo -e '\033[1;31mERROR: '"$1"'\033[0m'; }
shw_info "Check starting..."
find . -type f -exec wc -l \{\} 2>&1 \; | grep error
shw_info "Check finished"
@zipizap
zipizap / gist:7489927
Last active December 28, 2015 10:59
Detect ssh login from ~/.bashrc
# If you put this check inside ~/.bashrc you can detect bash session opened by ssh-logins :)
[ "$SSH_CONNECTION" -a "$SSH_TTY" == $(tty) ] && echo "This bash session was opened from an ssh login :)"