Cacomania: tag

Cacomania

Unix timestamp conversion with date

Guido Krömer - 1352 days ago - Tags: ,

If you are not smart enough to interpret a Unix timestamp by yourself date can do it for you.

$ date -u -d @1234567890
Fri Feb 13 23:31:30 UTC 2009

The timestamp can be formatted, too.

$ date -u -d @1234567890 +"%Y-%m-%d %T"
2009-02-13 23:31:30

If you need a timestamp, %s lets date print one.

$ date +"%s"
1386708117

Comment

Search all config files for a specific term

Guido Krömer - 1583 days ago - Tags: , ,

A common problem, which of all ini file contains the part loading module XYZ?

The find command below searches all ini files in the /etc folder for the term "mysqli".

$ find /etc -name "*.ini" -printf "echo \"FILE: %f\"\ngrep \"mysqli\" %h/%f\n" | bash

Comment

Monitor file copy progress with find and watch

Guido Krömer - 1626 days ago - Tags: ,

Currently copying a large amount of files and wants to see if the job is still running?

watch

find lists all files in the folder and his sub-folders, wc -l count the lines from stdout and watch executes this every two seconds.

$ watch "find ./ -type f | wc -l"

Comment

MySQL rename a database with bash

Guido Krömer - 1661 days ago - Tags: ,

Renaming an MySQL table is straightforward with RENAME TABLE, but renaming a whole database with RENAME DATABASE has been removed in MySQL 5.1.23 because it could be dangerous.

Since I needed this functionality some lines of bash does nearly the same by creating a new database and renaming each table like this: RENAME TABLE old_database.my_table new_database.my_table.

#!/bin/bash
execute_sql () {
  echo "$1" | mysql
}

rename_database () {
  FROM=$1
  TO=$2

  execute_sql "CREATE DATABASE IF NOT EXISTS $TO"

  TABLES=`execute_sql "SHOW TABLES FROM $FROM"`

  for TABLE in $TABLES; do
    execute_sql "RENAME TABLE $FROM.$TABLE TO $TO.$TABLE"
  done;

  echo "Done, drop the old database ($FROM) if needed."
}

rename_database $1 $2

Comment

Merging files together with the PowerShell

Guido Krömer - 1674 days ago - Tags: , ,

After playing a round with TypeScript, I needed a way for merging all those single files together. Performing this task with Bash is quite simple. Under Windows there is no Bash, but a thing called PowerShell. The PowerShell has got a large amount of Bash like aliases, which makes the move from Bash to PowerShel really simple, as you can see in the script below.

$TARGET_FILE = "Full.ts"

rm $TARGET_FILE 

$FILES = ls -Recurse -Filter "*.ts"

foreach ($FILE in $FILES) {
  cat $file.FullName >> $TARGET_FILE 
}

tsc $TARGET_FILE

Comment

Some bash special variables you should never forget

Guido Krömer - 1722 days ago - Tags: ,

Since the main topic here in the last time was JavaScript here is a small foray into the world of bash special variables. Although there are a lot more special variables in bash this might be the most common in my opinion.

Getting all params

There are two variables containing all params, the first one is $* which represents all params as single quoted string and $@ where each word is quoted separate. If it's not clear what is going on there, let's have a look at the example below.

#!/bin/bash
ALL_ARGS_STAR=("$*")
echo STAR: ${ALL_ARGS_STAR[0]}
echo STAR: ${ALL_ARGS_STAR[1]}
ALL_ARGS_AT=("$@")
echo AT: ${ALL_ARGS_AT[0]}
echo AT: ${ALL_ARGS_AT[1]}

Accesing the variables ALL_ARGS_STAR and ALL_ARGS_AT using the array indexer should make the difference of $* and $@ more clear.

[caco@MacBook-Air ~]# ./params-list.sh Foo Bar 1337
STAR: Foo Bar 1337
STAR:
AT: Foo
AT: Bar

The other usefull ones

  • $0, which is the first param and holds the called script name including the path.
  • $# represents the number of arguments.
  • $1 .. $n access the param directly.
  • $$ gets the PID of the running script.

If you got confused by $0, $#, $1337 or $$ have a look at the self explaining script below:

#!/bin/bash
SCRIPT_NAME=`basename $0`
NUM_ARGS=$#
PID=$$
ARG_ONE=$1
ARG_TWO=$2
ALL_ARGS=$@

echo "Script name: $SCRIPT_NAME"
echo "Number of arguments: $NUM_ARGS"
echo "PID: $PID"
echo "Arg one: $ARG_ONE"
echo "Arg two: $ARG_TWO"

i=1
echo -e "Arg:\tVal:"
for ARG in $ALL_ARGS; do
  echo -e "$i\t$ARG"
  i=$(( $i + 1 ))
done

Here is the generated output:

[caco@MacBook-Air ~]# ./bash_params.sh Foo Bar 123
Script name: bash_params.sh
Number of arguments: 3
PID: 25052
Arg one: Foo
Arg two: Bar
Arg:    Val:
1   Foo
2   Bar
3   123

Comment

FTP upload via cURL

Guido Krömer - 1837 days ago - Tags: , , ,

Uploading a file to a ftp server with cURL is really easy:

# curl -u user:pass -T my_file ftp://example.com/target/path/

Or with a little potion of bash for uploading multiple file:

# for file in ./*; do curl -u user:pass -T "$file" ftp://example.com/target/path/; done;

Comment

Updating DynDNS via Bash

Guido Krömer - 1845 days ago - Tags: , , ,

Because of my old wireless router was not able to update DynDNS, I spend a lot annoying time configuring the ddclient daemon on my home server. But the stability of this tool was really disappointing, after being blocked two times by dyndns, I decided writing my own script to update DynDNS frequently.

read more TEST

Comment [5]