Shell Notes

Frequently forgotten things

IF statements

  • String equality, note double ‘[[' here.
    #!/bin/bash
    i="Hello"  
    
      if [[ $i = "Hello" ]] ; then
         echo “Found hello”
      fi
    
    lovebox:[/tmp] $ ./1.sh
    Found hello
    
  • String exists.
    Note, only a single ']' used here.

    #!/bin/bash
    i="Hello"
      if [ $i ] ; then
         echo "Found hello"
      fi
    
    lovebox:[/tmp] $ ./1.sh
    Found hello
  • Variable does not exist
    #!/bin/bash
      if [ ! $i ] ; then
         echo "Hello is missing"
      fi
    
    lovebox:[/tmp] $ ./1.sh
    Hello is missing
    
  • Compare numeric values; Less than.
    if [[ $OPS -lt $TARGET ]] ; then 

    RunID's

    Generating runid's to generate unique file/directory names

  • Date and time
    RUNID=`date +"%y%m%d%H%M"`
  • Date and time and PID
    RUNID=`date +"%y%m%d%H%M"`-$$
    

    Loop structures

    Zsh
    % for i in {0..31}
    do
    echo lun resize /vol/vol1/lun$i 50456m
    done

    lun resize /vol/vol1/lun0 50456m
    lun resize /vol/vol1/lun1 50456m
    lun resize /vol/vol1/lun2 50456m
    lun resize /vol/vol1/lun3 50456m
    lun resize /vol/vol1/lun4 50456m
    lun resize /vol/vol1/lun5 50456m
    lun resize /vol/vol1/lun6 50456m
    lun resize /vol/vol1/lun7 50456m
    lun resize /vol/vol1/lun8 50456m
    lun resize /vol/vol1/lun9 50456m
    lun resize /vol/vol1/lun10 50456m
    ...
    
  • Leave a Reply