Category: Shell Scripting

Shell scripts and Linux Scripting Tutorials

Running interactive command or script over ssh

Running interactive command or script over ssh

In order to run interactive command or script which require you to input something , you can use -t option with ssh. eg. ssh -t x.y.z.a ‘sh /some/script.sh’

Using IFS variable in shell scripts

Using IFS variable in shell scripts

IFS variable comes handy when you want to split a string into array or create a loop. #!/bin/bash IFS=$’,’ dirs=’/usr/scripts,/var/lib,/usr/local’ for dir in $dirs; do echo $dir; #or do something here done unset IFS...

Bash one liners

Bash one liners

Rename multiple files: for i in *.dns; do mv “$i” “`basename $i .dns`.zone”; done or for i in *.JPG; do mv “$i” “${i/.JPG}”.jpg; done

Shell Script to Backup all tables separately

Shell Script to Backup all tables separately

#!/bin/bash SKIPDB=”mysql test” BACKUP_DIR=”/backup/dir” DBS=”$(mysql -Bse ‘show databases’)” for db in $DBS;do echo “Backing up Database:$db” mysqldump $db | /bin/gzip -9 > ${BACKUP_DIR}/${db}.gz echo “Backup complete for DB:$db” done

bash script to cd to a path

bash script to cd to a path

If you need to login to a server and frequently switch to a path, you might want to create a shell script to switch . eg. #!/bin/bash cd /path/to/where/you/want/to/go Now the trick is you...

Shell scripting tutorials

Shell scripting tutorials

Doing an Arithmetic : i=$((  i * 5 )) Single Quotes: Ignoring special Characters : echo ‘*’ [ anything inside single quotes is ignored ] Double Quotes: Same as single quotes but not restrictive....

Bash Script to check if a host is alive

Bash Script to check if a host is alive

Script to check if a host is alive: #!/bin/bash ping -c 2 www.amitnepal.com > /dev/null if [ $? -eq 0 ]; then echo “Alive”; else echo “Dead”; fi

Handling arguments in bash

Handling arguments in bash

#!/bin/bash echo Total Number of Arguments Passed: $# i=1; for var in “$@” do echo “Argument $i is $var” let i=$i+1; done   Check if expected number of arguments is passed to the script:...