Bash - Useful commands

ping -c 1 -w 1 -q
-c <x>: Stop sending after <x> packets.
-w <x>: Time to wait for a response, in seconds.
-q: Display summary line.

Bash - String Manipulation

Length of string

#!/bin/bash
str="What is the length of this string?"
echo "The length is ${#str}"
# Output:
# The length is 34


Search and Replace string

#!/bin/bash
str="1st dog. 2nd dog. 3r dog"
 
#Replace the 1st occurrence: ${string/find/replace}
str=${str/dog/cat}
echo "${str}"
# Output:
# 1st cat. 2nd dog. 3r dog
 
#Replace all occurrences: ${string//find/replace}
str=${str//dog/cat}
echo "${str}"
# Output:
# 1st cat. 2nd cat. 3r cat
#http://www.arachnoid.com/linux/shell_programming.html

Bash - Generate MD5 hash of all files under the provided path and all its subdirectories

#!/bin/bash
 
# Description: Generate MD5 hash of all files under the provided path and all its subdirectories.
# Author: Xuan Ngo
# Usage: thisScriptName.sh [path]
#        thisScriptName.sh .
#        thisScriptName.sh /some/absolute/path
#        thisScriptName.sh some/relative/path
# Output: YYYY-MM-DD HH:MM:SS | MD5 hash | Size in bytes | Filename
#############################################################################################
 
ProcessingPath="$1"
 
find $ProcessingPath -type f | while read filename ; do

Bash - Read input from file

#!/bin/bash
cat myfilename.txt |     # Supply input from a file.

Bash - Loops

For loop

#!/bin/bash
# Display each filename returned by ls.

Bash - Conditional Statements

NOTE: Spacing is very important on the IF statement line.

Bash - Get date and time

date_time=`date +%Y-%m-%d_%H.%M.

DOS - ECHO %DATE% %TIME% doesn't work in FOR loop

ECHO %DATE% %TIME% doesn't work in FOR loop. Here is an example:

Debian - Packages

All packages used.

Debian - VNC Server

  1. Install VNC Server.
      aptitude -y install vnc4server
     
  2. You must run VNC Server at least once to setup the password.
      vncserver
     

Pages

Subscribe to OpenWritings.net RSS