Robert Basic's blog

Posts tagged 'shell'

Find duplicate lines from a CSV

by Robert Basic on March 21, 2016.

Finding duplicate lines from a CSV file is something I have to do from time to time, yet not on a regular enough basis to remember it all. Plus, I’m trying to blog more often.

cut -d, -f1 file.csv | tr -d '"' | sort | uniq -dc

cut to split the lines at the commas and select the first field, then tr to delete any double quotes that encloses the field, then sort and finally with uniq to show only the duplicated lines and to prefix every line with the count of occurrences.

Tags: linux, shell.
Categories: Development.

Notes on shell scripting

by Robert Basic on December 29, 2011.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

Yesterday I did some shell scripting and thought about writing down the few things learned along the way. Amazing how little needs to be done to learn a lot :)

Result of a command to a variable

First thing I learned is how to “save” the result of a shell command to a local variable:

PHP_BINPATH=$(which php)

By enclosing the command in parenthesis and putting a dollar sign in front of it, will put the result of that command in the variable.

An empty variable

Turns out, a variable can be empty, null. Nothing strange with that, until one tries to do something with that variable. For example:

PHP_BINPATH=
if [ $PHP_BINPATH == "foo" ]
  then
    echo "It's foo"
fi

will die with a strange error: “line 2: [: =: unary operator expected”. Problem is that when evaluating it will see if [ == “foo” ] and turns out [ is some reserved command or some such. The fix is to wrap $PHP_BINPATH in double quotes:

PHP_BINPATH=
if [ "$PHP_BINPATH" == "foo" ]
  then
    echo "It's foo"
fi

Pass all the arguments!

When calling some other command from within the shell script, and all the arguments which are passed to the shell script need to be passed to that other command, use ”$@” for that:

$PHP_BINPATH usher.php "$@"

This will pass all the arguments to the PHP script which is called from within the shell script.

Happy hackin’!

Tags: bash, console, scripting, shell.
Categories: Development, Programming.

Change permissions on folders/files only

by Robert Basic on January 11, 2011.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

This is just a quick reminder for myself. Should really remember this one. Changes permissions on folders|files only.

$ find /path/to -type d -exec chmod 775 {} \;
$ find /path/to -type f -exec chmod 664 {} \;
Tags: permissions, shell.
Categories: Development.

Backup script for mysql

by Robert Basic on November 05, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

This post is more of a reminder for myself. Anywayz, a little bash script that backups a database, gzipit and deletes all backups older than 3 days.

#!/bin/bash

DBUSER="user"
DBPASS="pass"
DBDB="dbname"
NOW=$(date +"%Y-%m-%d-%H-%M-%S")
BACKUPROOTDIR="/tmp"
BACKUPSQL="$BACKUPROOTDIR/mysqlbackup-$NOW.sql"
BACKUPGZIP="$BACKUPSQL.gz"

mysqldump -u$DBUSER -p$DBPASS $DBDB > "$BACKUPSQL"
gzip -c $BACKUPSQL > $BACKUPGZIP
rm $BACKUPSQL
find $BACKUPROOTDIR -type f -name "mysqlbackup\*" -mtime +3 | xargs rm

Kudos to @zsteva for looking at it to spot any errors I might have made.

Tags: backup, mysql, script, shell.
Categories: Development.