Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

10.01.2018

Bash script for switching branches in Git

This script follows my workflow of switching between different branches in my dev environment:
1. Stash changes in my current branch.
2. Checkout the other branch.
3. Apply previously saved stash on the switched branch.
#!/bin/bash

echo "Switching to $1 branch..."
BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
STASHNAME="$BRANCH-changes"
git stash save "$STASHNAME"
git checkout $1
LATEST=$(git stash list | grep $1 | head -n 1)
echo "Latest stash: $LATEST"
echo "Do you want to apply this stash? [y/n]: "
read apply_stash
if [ "$apply_stash" == "y" ]; then
    STASHED=$(git stash list | grep $1 | head -n 1 | cut -d ":" -f1)
    git stash apply "$STASHED"
fi

7.20.2017

Change bash command prompt in Debian

Followed this post

$ vi /etc/bash.bashrc
Add this at the end:

# If id command returns zero, you have root access.
if [ $(id -u) -eq 0 ];
then # you are root, set red colour prompt
   PS1="\\[$(tput setaf 1)\\][\\u@\\h:\\w] (apps)# \\[$(tput sgr0)\\]"
else # normal
   PS1="[\\u@\\h:\\w] (apps)$ "
fi

7.21.2015

Bash Cheat Sheet

1. Run a script with no output and in the background
$ ./my_script.sh > /dev/null 2>&1 &

2.  Run a script with no output and in the background and see the pid
$ ./my_script.sh > /dev/null 2>&1 & echo $!

3. Template script that loops thru and writes to a file
#!/bin/bash

# $1 - number of minutes
# $2 - task id
COUNTER=$(($1*60/5))
TOTAL=$COUNTER
echo Perfload will run for $1 minutes
echo "Perfload will run for $1 minutes" > /tmp/$2.log
until [  $COUNTER -eq 0 ]; do
    echo The counter is $COUNTER
    PROGRESS=$(echo "scale=2;($TOTAL - $COUNTER)*100/$TOTAL" | bc -l)
    echo "Perfload RUNNING $PROGRESS %" >> /tmp/$2.log
    let COUNTER-=1
    sleep 5
done
echo "Perfload SUCCESS" >> /tmp/$2.log


1.06.2015

My .aliases

My collection of bash knick knacks.
alias 2proj='cd /auto/home/samdc/projects'

# cd to a directory that is relative to 2proj directory
function 2p() { 2proj && cd $@ ;}

# list resources files
# use: res filename*
function res() { 2proj && cd common/resources/ && ls -l */$@ ; }

# diff color
alias colorize='~/bin/p4c.rb'

6.05.2014

Schedule rake using cron on rvm environment

Little tricky to get this going. The key is knowing rvm environment.
$ rvm env --path
/auto/home3/delacs/.rvm/environments/ruby-2.0.0-p353@RailsDev
For rvm, basic command line tools like gem, rake and ruby are in the wrappers directory.
$ crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/auto/home3/delacs
RAILS_ENV=development

0 7 * * * /bin/bash -l -c 'source ~/.bash_profile' && cd $HOME/Documents/projects/monweb_management && $HOME/.rvm/wrappers/ruby-2.0.0-p353@RailsDev/rake perfdb:get_baselines >> $HOME/logs/db.import.log 2>&1

5 7 * * * /bin/bash -l -c 'source ~/.bash_profile' && cd $HOME/Documents/projects/monweb_management && $HOME/.rvm/wrappers/ruby-2.0.0-p353@RailsDev/rake perfdb:get_published_runs  >> $HOME/logs/db.import.log 2>&1

5.08.2014

Backup Dir and Files in Linux Using Tar and Bash

1. Create a bash script
$ vi backup.sh
#!/bin/bash
# This is a script the creates a backup of
# monitor instances: .../monitor/inst => .../monitor/backup

DATE=`date +"%Y%m%d"`
TIMESTAMP=`date +"%Y/%m/%d %T"`
FILENAME="inst_backup_$DATE.tar.gz"
SRCDIR="inst"
DESDIR="backup"

tar -cpzf $DESDIR/$FILENAME $SRCDIR

if [ $? -eq 0 ]; then
    echo "$TIMESTAMP: Backup success: $FILENAME"
else
    echo "$TIMESTAMP: Backup error: $?"
fi

# Any backup older than 30 days are deleted
find ./$DESDIR -type f -mtime +30 -delete -printf "$TIMESTAMP: Deleted %16f\n"

for tar:
c - create
p - preserve file and dir permission
z - compress file
f - target filename

2. Schedule with cron
$ crontab -e
0 5 * * * cd $HOME/scheduler/monitor && ./backup.sh >> $HOME/scheduler/monitor/backup/logs/monitor.backup.log 2>&1

This would schedule a backup at 5am everyday

9.24.2013

Bash Keyboard Shortcuts

Ctrl + A        Go to the beginning of the line you are currently typing on
Ctrl + E        Go to the end of the line you are currently typing on
Ctrl + L        Clears the Screen, similar to the clear command
Ctrl + U        Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H        Same as backspace
Ctrl + R        Let’s you search through previously used commands
Ctrl + C        Kill whatever you are running
Ctrl + D        Exit the current shell
Ctrl + Z        Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W        Delete the word before the cursor
Ctrl + K        Clear the line after the cursor
Ctrl + T        Swap the last two characters before the cursor
Esc + T         Swap the last two words before the cursor
Alt + F         Move cursor forward one word on the current line
Alt + B         Move cursor backward one word on the current line
Tab             Auto-complete files and folder names