Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

4 Jul 2014

checking system requirements for owncloud installation

Here a short bash script which checks for all php features required to install owncloud:

#!/usr/bin/env bash

# (>= 5.3.8 minimum, 5.4 or higher recommended)
version=$(php -v | head -n 1 | sed 's/PHP \([0-9.]\+\).*/\1/')
echo "PHP Version $version installed."

# check for the following features:
features="ctype dom GD iconv JSON libxml mbstring SimpleXML zip zlib"

for f in $features; do
 echo -en "Checking: $f";
 r=$(php -i | egrep -i '^'$f'$')
 if [[ "$r" != "" ]]; then 
  echo " ... Found"; 
 else
  echo "";
 fi
done

7 Dec 2013

#! shebang for C programs

For testing it is sometimes very tedious to always compile and run the small C programs I use as utilities. So I thought, why not use the shebang for this. After investigating how the shebang mechanism works I figured a small bash script would do the trick.

The following script needs to be somewhere in your $PATH, I call the script "C":

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env bash

filename=$(basename "$1")
filename="${filename%.*}"

# need to compile?
if [ "$1" -nt "/tmp/runcache/$filename" ]; then
 mkdir -p /tmp/runcache/
 # get rid of shebang line in C program and compile it
 tail -n +2 "$1" > /tmp/runcache/$filename.c
 gcc -Wall -std=c99 $CC_OPTS -o \
  /tmp/runcache/$filename \
  /tmp/runcache/$filename.c
fi

# first parameter is the old executable, 
# remove it and poitn to the new one
shift 

/tmp/runcache/$filename "$@"

Once in place, you can add the shebang line "!#/usr/bin/env C" to your C code in the first line, make it executable and run it like a script. Here is our test C program:


1
2
3
4
5
6
7
8
#!/usr/bin/env C 

#include <stdio.h>

int main() {
 printf("Hello World\n");
 return 0;
}

Then run it like this:

$ chmod 700 test.c
$ ./test.c
Hello World
$


4 Feb 2013

nag-o-meter



This is our new status display of the Service Monitoring System I have built up during the last years. My cow-orkers keep forgetting to act upon warnings. I thought I could add some extra motivation for fixing errors in a timely manner (note the nice sound it makes :) ).

A little bit of python, a little bit of *nix voodoo glued together with a bash script - done.



28 Jun 2012

Compile and Run a C file from wihin gedit

I use gedit to write simple C programs. The External Tools plugin is really helpful for compiling and running them. Here is a simple example to invoke gcc and run the compiled file:

#!/bin/sh

EHOME=`echo $HOME | sed "s/#/\#/"`
DIR=$GEDIT_CURRENT_DOCUMENT_DIR
BASENAME=`echo "$GEDIT_CURRENT_DOCUMENT_NAME" | sed -e 's/\.c$//'`

cd "$GEDIT_CURRENT_DOCUMENT_DIR"
date
gcc -o "$BASENAME" "$GEDIT_CURRENT_DOCUMENT_NAME"

26 Oct 2011

Export SVG vector graphic to different sizes of PNG

#!/usr/bin/env bash

function export() {
 width=$1
 height=$2
 
 inkscape -w $width -h $height \
  -e app_icon_${width}x${height}.png app_icon.svg
}

export 72 72
export 48 48
export 36 36

5 May 2011

subversion and xxdiff

xxdiff is an X11 application for displaying differences of two files.

Installation on Debian:
$ sudo apt-get install xxdiff-scripts

Example usage with subversion (if you run the above command and are on X11 with xxdiff installed and in your $PATH, xxdiff should start, else console output should appear):

#!/usr/bin/env bash
# $Id: svndiff_gui.sh 174 2011-05-05 19:13:55Z wus $

# use xxdiff when available and on X11

if [[ -n "$DISPLAY" && -x `which xxdiff` ]]; then
 svn stat
 read -p "Graphical diff? [Y/n]: " proceed
 if [[ "$proceed" == "" || "$proceed" == "y" || "$proceed" == "Y" ]]; then
  xx-svn-diff $@
  exit $?
 fi
fi

svn diff $@

5 Sept 2010

Hmmm, connects on port 8080, what are they looking for ?

Lately I get many connects on port tcp/8080 at home. Sounds like someone is looking for an open proxy?

It's simple to monitor the traffic with minimal unix tools (even without a gui):

while true; do netcat -l 8080 >> 8080.log; done&

tail -f 8080.log
==> 8080.log <==

Now sit back and watch the log.

9 Aug 2010

selecting text in vim from an xterm

I have line numbers turned on in vim. this makes selecting text awkward, since the line numbers are selected and copied too.

Fortunately, there is a helpful vimrc setting as long as you are using some sort of xterm:

set number
set mouse=a
Left-clicking and dragging in a terminal in which vim is running will result in text a only selection.

29 Jan 2010

WTF, why can't I unmount a volume?

Sometimes some file locks are preventing unmounting a block devices (especially annoying on removable devices).

Some nice utilities are fuser (list processes using files, sockes or fs) and lsof (list open files).

This combined with kill gets those external block devices quickly unmounted when needed

packing several unix files into one executable shell script (simple installer)

These 2 scripts are able to pack a directory into a shell script. The shell script can then be executed on another system and will extract the packed files into a target directory, unix permissions will be preserved (tar is used).

What you need:
- sh
- readlink
- dirname
- basename
- tar
- gzip
- uuencode
- uudecode
- cat
- chmod

pack.sh

#!/bin/sh

# $1 is source directory or file
if [ -z "$1" -o ! -e "$1" ]; then
echo "Usage: $0 <target-directory-or-file> <destination-file>"
exit 1
fi

# $2 is dst file
if [ -z "$2" ]; then
echo "Usage: $0 <target-directory-or-file> <destination-file>"
exit 1
fi

# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`

WORKINGDIR="$1"
TARGET="."
if [ -f "$WORKINGDIR" ]; then
WORKINGDIR=`dirname $WORKINGDIR`
TARGET=`basename $1`
fi

# pack directory or files into a uuencoded tar.gz archive
cat "$SCRIPTPATH/execute.sh" > "$2"
echo "w $WORKINGDIR $TARGET"
tar -c -C "$WORKINGDIR" -zf - "$TARGET" | \
uuencode "embedded-files.tar.gz" >> "$2"

chmod u+x,g+x "$2"


execute.sh
#!/bin/sh

# $1 is target directory
if [ -z "$1" ]; then
echo "Usage: $0 <target-directory>"
exit 1
fi

# create target directory (if not exists) or exit if we fail
mkdir -p "$1"
if [ $? != 0 ]; then
echo "Failed to create target directory"
exit 3
fi

# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`

cd $1;
uudecode "$SCRIPTPATH/$(basename $0)"
tar -xzf "embedded-files.tar.gz"
rm "embedded-files.tar.gz"

exit 0;