Pages

Thursday, April 21, 2016

Linux Bash Scripts - Exercise

  • Create a script "/tmp/string_input_validator.sh" with executable by all such a manner 
        - with input "hello" output should be "Welcome to string validator"
        - with input "bye" output should be "Thank you for using string validator"
        - with input any other value output should be "hello|bye"
        - with no input, output should be "No argument provided, script requires argument"
  • Create a script to convert time-stamp in EPOC/UNIX format to normal format
        - with input EPOC/UNIX format value (i.e.  1448337029  ) output should be corresponding normal time stamp
        - with input any other value output should be "Input not in EPOC/UNIX format"
        - with no input, output should be "No argument provided, script requires argument"
  • Create a script to convert time-stamp in normal format to EPOC/UNIX format 
        - with input normal format value (i.e.  23 Nov 2015 09:50:29   24 hour clock ) value output should be corresponding EPOC/UNIX stamp
        - with input any other value output should be "Input not in normal format value (i.e.  23 Nov 2015 09:50:29   24 hour clock )"
        - with no input, output should be "No argument provided, script requires argument"
  • Create a script to find difference between two normal format time stamp values  
        - with input two normal format value (i.e.  23 Nov 2015 09:50:29   24 hour clock ) values output should be difference in HH:MM:SS format (i.e. hours:minutes:seconds)
        - with only one input value, output should be "One argument provided, script requires two arguments"
        - with no input, output should be "No argument provided, script requires two arguments"
  • Create a script /tmp/create_bulk_users.sh for creating users from userlist.cfg file.
        - Create /tmp/userlist.cfg file with below content.
           user1
           user2
           user3
        - when userlist as first argument provided it will create all the users as per users name specify in userlist.cfg file and all users should have login shell set to /bin/nologin. 
        - if other argument provided it will display "Invalid file name" output.
        - if not providing any argument it will display "No argument provided, script requires argument"
  • Create a script to display type of a file provided in input
        - when input is provided as absolute path of file name output should be "File _____ is a XXXX file" where XXXX can be Link File/Regular File/Block File/Character File/Socket File/Named Pipe File and _______ is the input filename
        - input should be always file name with absolute path 
        - if input file does not exists output should be "File ______ not found", where _______ is the input filename
        - if not providing any argument it will display "No argument provided, script requires argument"
  • Create a script to display details of user provided in input
        - Input to script is user name, based on provided input  script should check for presence of that user on server
        - if user not present it should give message as "User ______ not present on server"
        - if user is found then it should display below information
          UID, GID, home directory, login shell.
          User's account expiry date
          User's password expiry date
  • Create a script for file system backup with below criteria
        - Backup should be taken on central server
        - Backup is to be taken daily at 9 AM IST and two copied of recent backups are to be maintained
        - Script should be able to read Remote server IP and Remote server directory to be backed up from a configuration file in order to handle future additions and removals
        -  Script should send an email alert for success and failure of backup     
  • Create a script for MySQL database instances backup
        - Backup should be generated using mysqldump command
        - Backup is to be taken daily at 9 AM IST and two copied of recent backups are to be maintained
        - Script should be able to read database name, socket file, port, username password of database to be backed up from a configuration file in order to handle future additions and removals
        -  Recent 10 database backups should be maintained by script      
        -  Script should send an email alert for success and failure of backup        
  • Create a script httpd restart
        - Script should be scheduled in and to be executed using normal user
        - Script should run on alternate days at 5AM IST
        - Script should capture logs of cron and maintain them for historical logs checkup.
        - Script should send an email alert for failure in httpd restart    
  • Create a Script to backup file or folder in a folder on local machine from a folder hosted on local machine
        - input argument usage
        - input argument validation
        - cp/rsync command usage

  • Create a Script to backup file or folder in a folder on local machine from a folder hosted on local machine

        - read input from a file
        - input value validation
        - cp/rsync command usage
        - while/for loop usage

  • Create a Script to backup file or folder in a folder on local machine from a folder hosted on remote machine 

        - input argument usage
        - input argument validation
        - scp/rsync command usage

  • Create a Script to backup file or folder in a folder on local machine from a folder hosted on local machine

        - read input from a file
        - input value validation
        - cp/rsync command usage
        - while/for loop usage

  • Creat a Script to backup file or folder in a folder on local machine from a folder hosted on local/remote machine

        - input argument usage
        - input argument validation
        - cp/scp/rsync command usage based on validation of destination type and path

  • Create a Script to backup file or folder in a folder on local machine from a folder hosted on local/remote machine

        - read input from a file
        - input values validation
        - cp/scp/rsync command usage based on validation of destination type and path
        - while/for loop usage

  • Create a Script to backup file or folder in a folder on remote machine from a folder hosted on local/remote machine

        - input argument usage
        - input argument validation
        - cp/scp/rsync command usage based on validation of destination type and path

  • Create a Script to backup file or folder in a folder on remote machine from a folder hosted on local/remote machine

        - read input from a file
        - input values validation
        - cp/scp/rsync command usage based on validation of destination type and path
        - while/for loop usage

  • Create a Script to backup file or folder in a folder on local/remote machine from a folder hosted on local/remote machine

        - input argument usage
        - input argument validation
        - cp/scp/rsync command usage based on validation of source and destination type and path

  • Create a Script to backup file or folder in a folder on local/remote machine from a folder hosted on local/remote machine

        - read input from a file
        - input values validation
        - cp/scp/rsync command usage based on validation of source and destination type and path
        - while/for loop usage

Home

Linux Command One Liners - Exercise


  • To empty a file or truncate a file to 0 size
  • To replace content of a file
  • To append content to a file as a new line
  • To append content to a file without new line (by continuing last line )
  • To append content to a file 
  • To read first line of a file and store it in a variable 
  • To read a file line by line and store it in a variable
  • To read random line from a file and store it in a variable
  • To read only 3 columns/fields form a file into variables
  • To find number of lines, words, characters in a file and store it in a variable
  • To find value in 2nd and 5th column of a one line variable and store it in a variable
  • To find size of file and store it in a variable
  • To make a copy of file
  • To move or rename a file
  • To generate Small letter a to z (with spaces between characters)
  • To generate Capital letter A to Z (with spaces between characters)
  • To generate Small letter a to z (without spaces between characters)
  • To generate Capital letter A to Z (without spaces between characters)
  • To generate all letters in a column output and store it in a variable
  • To generate numbers from 1 to 100 (with spaces between numbers)
  • To generate numbers from 1 to 100 (in column format)
  • To generate numbers 0 to 9 with a leading zero as pad
  • To generate n number of words 
  • To generate alphanumeric strings
  • To generate 5 copies of the same string
  • To join two strings/variables
  • To Split a string on a given character
  • To replace only first occurrence of string 
  • To replace only all occurrences of string 
  • To find the length of the string
  • To extract a substring from a string
  • To uppercase a string
  • To lowercase a string
  • To redirect the standard output of a command to a file
  • To redirect the standard error of a command to a file
  • To redirect both standard output and error of a command to a file
  • To discard standard output of a command to a file
  • To discard standard error of a command to a file
  • To discard standard output and error of a command to a file
  • To redirect contents of a files to the standard input of a command 
  • To redirect list of strings i.e. multiline to the standard input of a command 
  • To redirect a single line of text to the standard input of a command 
  • To redirect standard output of all command 
  • To redirect standard error of all command 
  • To redirect standard output and error of all command 
  • To send the output from multiple commands to a file
  • To use exec command in script to capture all standard error and standard output  in separate file 
  • To prevent overwriting the contents of a file when redirecting output
  • To redirect standard input to a file and print it to standard output
  • To send standard output of one command to standard input of another command
  • To send standard output and error of one command to standard input of another command
  • How order of redirection matters
  • To swap standard output and standard error
  • To send standard output to one command and standard error to another command
  • To find the exit codes of all piped commands
  • To erase all shell history
  • To stop logging history for this session
  • To change log file for capturing command history 
  • To add timestamp to history logs
  • To show history
  • To repeat last executed command
  • To execute the most recent command starting with the given string
  • To open the previous command you executed in a text editor
  • Use short-keys on shell prompt to move to beginning of the line
  • Use short-keys on shell prompt to move to end of the line
  • Use short-keys on shell prompt to move the cursor “one word” backward
  • Use short-keys on shell prompt to move the cursor “one word” forward
  • Use short-keys on shell prompt to delete “last word”
  • Use short-keys on shell prompt to undo recently deleted “last word”
  • Use short-keys on shell prompt to move the cursor “one character” backward
  • Use short-keys on shell prompt to move the cursor “one character” forward
  • Use short-keys on shell prompt to delete “whole line”
  • Use short-keys on shell prompt to search the history backward
  • Use short-keys on shell prompt to search the history forward
  • Use short-keys on shell prompt to swap “two adjacent characters” 
  • Use short-keys on shell prompt to swap “two adjacent words” 
  • Use short-keys on shell prompt to convert characters next to cursor location in a word to uppercase 
  • Use short-keys on shell prompt to convert characters next to cursor location in a word to lowercase 
  • Use short-keys on shell prompt to capitalized a character in a word
  • Use short-keys on shell prompt to comment the current line (i.e. append # at the beginning quickly)
  • Use short-keys on shell prompt to delete “one character” to the left
  • Use short-keys on shell prompt to delete “one character” to the right
  • Use short-keys on shell prompt to comment the current line (i.e. append # at the beginning quickly)
  • Use short-keys on shell prompt to undo last action 
  • Use short-keys on shell prompt to undo all changes to a line
  • Use short-keys on shell prompt to clear screen
  • To list CRON jobs for a user
  • To edit CRON jobs for a user
  • To schedule a job which will run every minute for capturing number of processes running for each user on system
  • To schedule a job which will run on each 12 min for capturing number of open files on system for against each user 
  • To schedule a job which will run hourly for capturing user’s logged in at that moment
  • To schedule a job which will run twice in a day for checking filesystem utilization and through alert for utilization more than 40%
  • To schedule a job which will run alternate day at 9 AM for backup of a directory by keeping latest 3 copies including current one
  • To schedule a job which will run each alternate Sunday for calculating top 10 users having home directory usage high.
  • To schedule a job which will run on first minute of each year to capture uptime of system
  • To print date in terminal default format
  • To print Date in terminal GMT/UTC time zone
  • To set a date
  • To display Month, Day and Year
  • To display Hours, Minutes and Seconds
  • To find the number of seconds from unix epoch.
  • To display date with customized text
  • To execute a job in the background and get it in foreground again (dependent on current session)
  • To execute a job in the background and get it in foreground again (not dependent on current session)
  • To display calendar of Sept, 1972 
  • To display calendar of first three months of 2008
  • To display calendar of complete year 1999
  • To toggle between the last two directories cd -
  • To go to home directory cd ~
  • To check permissions summary for a folder i.e. list of possible users, group and permissions with count of each possibility

Home

How Cran-R and Hadoop are related ?

Tuesday, April 12, 2016

working with git and gitlab


Installation of GITLAB :-


LDAP Configuration of GITLAB :-
http://doc.gitlab.com/ee/administration/auth/ldap.html
https://raymii.org/s/tutorials/Gitlab_and_Active_Directory_LDAP_Authentication.html


Working with GIT

https://www.digitalocean.com/community/tutorials/how-to-use-the-gitlab-user-interface-to-manage-projects
https://www.digitalocean.com/community/tutorials/how-to-install-git-on-ubuntu-14-04
https://www.digitalocean.com/community/tutorials/how-to-use-git-effectively
https://www.digitalocean.com/community/tutorials/how-to-use-git-branches
https://about.gitlab.com/2015/02/19/8-tips-to-help-you-work-better-with-git/


Core References :-
https://githowto.com
https://git-scm.com/book/en/v2

mail command one liners

Home

For sending email to a single user
# echo "Test of Mail body" | mail -s "Mail subject" to@somedomain.com

Where,
echo command is used for specifying body of the email.
-s option is used for specifying the mail subject
to@somedomain.com is the ens user’s email ID

Re-write above command for multi recipients

# echo "Test of Mail body" | mail -s "Mail subject" recipient1@somedomain.com  recipient2@somedomain.com

For sending contents of a text file

# cat  somefile.txt  | mail -s "Mail subject" "user1@example.com,user2@example.com"

# mail -s "Mail subject" "user1@example.com,user2@example.com" < somefile.txt

For sending email with CC & Bcc

# echo "something" | mailx -s "subject" -b bcc_user@unixmantra.com -c cc_user@some.com  -r sender@some.com recipient@example.com

# mail -s "Mail subject" -c "cc_user@some.com" -b "bccuser@yahoo.com" "user@example.com" < somefile.txt

For sending email by attaching files:


# uuencode attachment-file attachment-file | mail -s "Mail subject" "to-user@example.com" < somefile.txt


mpstat command one liners


By default mpstat displays CPU statistics

# mpstat

To display all information about CPU

# mpstat -I ALL -u -P ALL
# mpstat –A

To display CPU statistics of individual CPU (or) Core

# mpstat -P ALL

To display statistics information of a particular CPU (or core)

# mpstat -P 0

# mpstat -P 1

To check version of mpstat


# mpstat -V


vmstat command one liners


vmstat by default will display the memory usage (including swap)

# vmstat

Where output of vommand can be understood as below,

    Procs – r: Total number of processes waiting to run
    Procs – b: Total number of busy processes
    Memory – swpd: Used virtual memory
    Memory – free: Free virtual memory
    Memory – buff: Memory used as buffers
    Memory – cache: Memory used as cache.
    Swap – si: Memory swapped from disk (for every second)
    Swap – so: Memory swapped to disk (for every second)
    IO – bi: Blocks in. i.e blocks received from device (for every second)
    IO – bo: Blocks out. i.e blocks sent to the device (for every second)
    System – in: Interrupts per second
    System – cs: Context switches
    CPU – us, sy, id, wa, st: CPU user time, system time, idle time, wait time

To display active and inactive memory

# vmstat -a

To display number of forks since last boot. This displays all fork, vfork, and clone system call counts.

# vmstat -f

To execute vmstat every 2 seconds, you have to press Ctrl-C to stop this.

# vmstat 2

To execute vmstat every 2 seconds for 10 times

# vmstat 2 10

To display timestamp

# vmstat -t 1 100

To display version.

# vmstat –V

To display statistics in a table format

# vmstat -s

To display disk statistics

# vmstat -d

To increase the width of the display
(default without setting width)
# vmstat 1 3
(Setting width)
# vmstat -w 1 3

To display statistics for a partition

# vmstat -p sdb1

To display in statistics in MB


# vmstat -S m

iostat command one liners

Home

To displays the device I/O statistics in Blocks. (default option)

# iostat

To display only the CPU usage statistics

# iostat –c

To display only the disk I/O statistics

# iostat -d

To displays only the device and NFS statistics

# iostat -n

To displays the device I/O statistics in Blocks in MB

# iostat -m

To displays I/O data for specific disks available in the system

# iostat -p sda

To display the current time in iostat

# iostat -t

To display extended status

# iostat -x

To display extended information for a specific partition

# iostat -x sda1

To execute iostat every 2 seconds (until you press Ctl-C)

# iostat 2

To execute every 2 seconds for a total of 3 times

# iostat 2 3

To display the LVM statistics

# iostat -N

To display the version of iostat


# iostat -V


Back To Top
Home

Deleting mail in postfix queue (mailq)

Postfix stores mails in a queue before sending it. Sometimes you wish to remove the mails from the queue but wonder how. Postfix has a command line called postsuper which can be used to delete unsent mails from the queue. Before removing the mail from the queue it is good idea to list all mails in the queue. By issuing the command:

# mailq

You will list all of the mails which are queued or simply timed out for some reason.
If you want to remove a mail from the "mailq" type:

postsuper -d mailID

Where mailID is the ID of the mail in the mail queue.
Issuing the command:

postsuper -d ALL


Will delete all queued mails from the mailq.

Sunday, April 10, 2016

getopts one liners

Home

The base-syntax for getopts is:
getopts OPTSTRING VARNAME [ARGS...]
where:
OPTSTRING - tells getopts which options to expect and where to expect arguments (see below)
VARNAME - tells getopts which shell-variable to use for option reporting
ARGS - tells getopts to parse these optional words instead of the positional parameters

The option-string tells getopts which options to expect and which of them must have an argument. The syntax is very simple — every option character is simply named as is, this example-string would tell getopts to look for -f, -A and –x

getopts fAx VARNAME

When you want getopts to expect an argument for an option, just place a : (colon) after the proper option flag. If you want -A to expect an argument (i.e. to become -A SOMETHING) just do:

getopts fA:x VARNAME

If the very first character of the option-string is a : (colon), which indicate getopts to switch to "silent error reporting mode". In productive scripts, this is helpful to handle errors yourself without being disturbed by annoying messages.

getopts :fA:x VARNAME


Example script getopts with only single mandatory argument

#!/bin/bash
while getopts ":a:" opt; do
  case $opt in
    a)
      echo "-a was triggered, Parameter: $OPTARG" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

Executing script with below style will not give you any output as below,

# sh getops-01.sh
#

# sh getops-01.sh /etc/passwd
#

Executing script with option will show you various outputs as below,

# sh getops-01.sh -a
Option -a requires an argument.

# sh getops-01.sh -b
Invalid option: -b

# sh getops-01.sh -c
Invalid option: -c

# sh getops-01.sh -a 123
-a was triggered, Parameter: 123

# sh getops-01.sh -a 123 -a -a -c
-a was triggered, Parameter: 123
-a was triggered, Parameter: -a
Invalid option: -c

Example script getopts with two mandatory arguments

#!/bin/bash
while getopts ":a:b:" opt; do
  case $opt in
    a )
      echo "-a was triggered, Parameter: $OPTARG"

      ;;
    b )
      echo "-b was triggered, Parameter: $OPTARG"
      ;;
    \? )
      echo "Invalid option: -$OPTARG"
      exit 1
      ;;
    : )
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;
  esac
done

Executing script with below style will not give you any output as below,

# sh getops-02.sh
#

# sh getops-02.sh /etc/passwd
#

Executing script with option will show you various outputs as below,

# sh getops-02.sh -a
Option -a requires an argument.

# sh getops-02.sh -a -b
-a was triggered, Parameter: -b

# sh getops-02.sh -a 123
-a was triggered, Parameter: 123

# sh getops-02.sh -a 123 -b
-a was triggered, Parameter: 123
Option -b requires an argument.

# sh getops-02.sh -a 123 -b 234
-a was triggered, Parameter: 123
-b was triggered, Parameter: 234

# sh getops-02.sh -a 123 -b 234 -c
-a was triggered, Parameter: 123
-b was triggered, Parameter: 234

Invalid option: -c

References:-
http://wiki.bash-hackers.org/howto/getopts_tutorial

Back To Top
Home