bash

Bash may refer to: read more at WikiPedia

  • oracle_logo3

    The Oracle WebLogic 11g application server product line is the industry's most comprehensive Java platform for developing, deploying, and integrating enterprise applications. It provides the foundation for application grid, which is an architecture that enables enterprises to outperform their competitors while minimizing operational costs.

    Some notes

    This script may look unsecure (and it is)

    Since password are store inside (admin server login: admin and password is admin), by doing so I can restart Oracle Weblogic in a cron job since stopping wont query the console for the login and password: feel free to remove these lines in blue

    I always recommend to install web process in its own user group and use a dedicated user to mitigate any securities issues:

    # groupadd weblogic
    # useradd -g weblogic-c weblogicuser for weblogic' -m weblogic
    # su – weblogic

    Install then weblogic in /home/weblogic

    Respect the order of component for starting

    1. Start Weblogic Node Manager,
    2. Start WebLogic Admin server,
    3. Start all Managed Server in any order.

    and stopping components

    1. Stop the Node Manager
    2. Stop all Managed WebLogic server
    3. Stop WebLogic

    Names of the managed server

     managed server names are in the script so add remove start and stop command for them

    Logs files of start and stop operations

    are written in 2 files, that use timestamp, see WLS_LOG_START and WLS_LOG_STOP

    /etc/init.d/weblogic file

    Create a new file as root in /etc/init.d/weblogic

    # vi /etc/init.d/weblogic

    and paste inside the following

    #!/bin/sh
    # description: webLogic adminServer and managedServer start script
    #

    # customized below to your likings
    WLS_DOMAIN=mytestdomain
    WLS_BASE=/home/weblogic/
    WLS_HOME=${WLS_BASE}/bea/user_projects/domains/$WLS_DOMAIN
    WLS_NODE_HOME=${WLS_BASE}/bea/wlserver_10.3/server/bin
    WLS_OWNER=
    weblogic
    WLS_ADMIN_PORT=7001
    WLS_ADMIN_LOGIN=admin
    WLS_ADMIN_PWD=admin
    WLS_LOG_START=${WLS_BASE}/logs/start.`date '+%d%m%y'`.log
    WLS_LOG_STOP=${WLS_BASE}/logs/stop.`date '+%d%m%y'`.log
    WLS_MANAGED_SERVER1=dev
    WLS_MANAGED_SERVER2=test
    JAVA_OPTIONS="${JAVA_OPTIONS} -Dweblogic.management.username=${WLS_ADMIN_LOGIN}"
    JAVA_OPTIONS="${JAVA_OPTIONS} -Dweblogic.management.password=${WLS_ADMIN_PWD}"

    export JAVA_OPTIONS

    if [ ! -f $WLS_HOME/startWebLogic.sh ]
    then
        echo "WebLogic startup: cannot $WLS_HOME/startWebLogic.sh "
        exit
    fi

    startWeblogic()
    {
    su - $WLS_OWNER -c "nohup $WLS_NODE_HOME/startNodeManager.sh > ${WLS_LOG_START} 2>&1 &"
    sleep 10
    su - $WLS_OWNER -c "nohup $WLS_HOME/startWebLogic.sh >> ${WLS_LOG_START} 2>&1 &"
    sleep 10
    su - $WLS_OWNER -c "nohup $WLS_HOME/bin/startManagedServer.sh >> ${WLS_LOG_START} 2>&1 &"
    sleep 10
    su - $WLS_OWNER -c "nohup $WLS_HOME/bin/startManagedServer.sh >> ${WLS_LOG_START} 2>&1 &"
    return 0
    }

    stopWeblogic()
    {
    su - $WLS_OWNER -c "nohup $WLS_NODE_HOME/stopNodeManager.sh > ${WLS_LOG_STOP} 2>&1 &"
    sleep 10
    su - $WLS_OWNER -c "nohup $WLS_HOME/bin/stopManagedWebLogic.sh $WLS_MANAGED_SERVER1t3://localhost:$WLS_ADMIN_PORT ${WLS_ADMIN_LOGIN} ${WLS_ADMIN_PWD} >> ${WLS_LOG_STOP} 2>&1 &"
    sleep 10
    su - $WLS_OWNER -c "nohup $WLS_HOME/bin/stopManagedWebLogic.sh $WLS_MANAGED_SERVER2 t3://localhost:$WLS_ADMIN_PORT  ${WLS_ADMIN_LOGIN} ${WLS_ADMIN_PWD}>> ${WLS_LOG_STOP} 2>&1 &"
    sleep 10
    su - $WLS_OWNER -c "nohup $WLS_HOME/bin/stopWebLogic.sh >> ${WLS_LOG_STOP} 2>&1 &"
    return 0
    }

    case "$1" in
        'start')
            startWeblogic
            ;;
        'stop')
            stopWeblogic
            ;;
        'restart')
            stopWeblogic
            startWeblogic
            ;;
        *)
            echo "Usage: $0 start|stop|restart"
            exit 1
            ;;
    esac

  • subversion.logo

    If you are using Subversion/CVS, you might have come across the issue where multiple developers working on a set of files are committing without any comments.

    Subversion’s hook scripts provide a powerful way to associate actions with repository events. For example, the pre-commit hook allows you to check — and possibly abort — a transaction before it actually gets committed. I will provide you now two easy Unix bash scripts  that avoid bad developer behaviors:

    • The first one “checkCommentNotEmpty.sh” is for checking that nobody submit an empty SVN commit comment
    • The second one “checkCommentSyntax.sh” is able to enforce SVN commit comment pattern using regular expressions

    Creating and Installing a Hook Script

    Your Subversion repository already has some template hook scripts. For example, the pre-commit template is in PATH_TO_REPOS/hooks/pre-commit.tmpl. These templates contain instructions on what the hook script does and what parameters it can expect.

    You can hook your own script on the following events

    • start-commit Before the commit transaction starts
    • pre-commit After the commit transaction starts but before the transaction is committed
    • post-commit After the commit transaction completes
    • pre-revprop-change Before a revision property is changed Repository Path,
    • post-revprop-change: After a revision property is changed Repository Path
    • pre-lock: Before the lock being acquired
    • post-lock: After the lock being acquired

    You’ll find examples in the hook directory named

    • post-commit.tmpl
    • pre-unlock.tmpl
    • post-lock.tmpl
    • pre-commit.tmpl
    • start-commit.tmpl
    • post-revprop-change.tmpl
    • pre-lock.tmpl
    • post-unlock.tmpl
    • pre-revprop-change.tmpl

    If you cant find them, brute force your server to locate them

    # find / –name pre-commit

    or

    # locate pre-commit

    On Debian you will find them at

    /data/svn-repos/{yourRepository}/hooks

    Create a file pre-commit with that content, nothing force you to put a huge bash script in pre-commit. In my example below, I prefer to divide pre-commit checks in multiple files. The pre-commit hook gives you a way to catch the transaction before it becomes a revision. Subversion passes this hook two parameters:

    1. the path to the root of the repository
    2. the transaction identifier
    #!/bin/sh
    set -e
    /data/svn-repos/{yourRepository}/hooks/checkCommentNotEmpty.sh "$1" "$2"
    /data/svn-repos/{yourRepository}/hooks/checkCommentSyntax.sh "$1" "$2"

    Note that after installation, every commit will run these two scripts. Check your permissions, scripts have to be runnable for the user (www-data on debian)

    Avoid empty comment in SVN commits

    Save this file in /data/svn-repos/{yourRepository}/hooks/checkCommentNotEmpty.sh

    #!/bin/sh
    REPOS="$1"
    TXN="$2"
    SVNLOOK=/usr/bin/svnlook
    
    if [`$SVNLOOK log -t $TXN $REPOS` != ""]; then
      echo "" 1>&2
      echo "*** Your commit has been blocked because you did not give any log message or your log message was too short." 1>&2
      echo "Please write a log message describing the purpose of your changes and then try committing again." 1>&2
      exit 1
    else
      exit 0
    fi

    Example in Eclipse, if you break the rule

    noEmptySVNCommitComment

    Enforce SVN commit comment pattern using regular expressions

    Save this file in /data/svn-repos/{yourRepository}/hooks/checkCommentSyntax.sh

    This script use Bash script REGEX capabilities, I check against what could be a typical JIRA issues entry

    If any developer try to use a commit statement not starting with for example PRODUCT-xxxx, the commit will be blocked.

    #!/bin/sh
    REPOS="$1"
    TXN="$2"
    SVNLOOK=/usr/bin/svnlook
    regex="PRODUCT-[0-9]*"
    
    if [[ `$SVNLOOK log -t $TXN $REPOS` =~ ${regex} ]]; then
      exit 0
    else
      echo "" 1>&2
      echo "*** Your commit has been blocked because you give an invalid commit comment" 1>&2
      echo "Please make your commit comment start with PRODUCT-XXX" 1>&2
     exit 1
    fi

    Example in Eclipse, if you break the rule

    checkSVNCommitCommentWithRegularExpressions

    Want more?

    You can automated code reviews with Checkstyle using pre-commit script and so Stop rule-breaking code before it enters your code base!

    References

    hook_scripts" target="_blank">http://subversion.tigris.org/tools_contrib.htmlhook_scripts List of some Hook scripts examples written in python/bash

  • First let's refresh some definitions...
    set user ID (SUID)

    The SUID permission causes a script to run as the user who is the owner of the script, rather than the user who started it. It is normally considered extremely bad practice to run a program in this way as it can pose many security problems.

    set group ID (SGID)

    The SGID permission causes a script to run with its group set to the group of the script, rather than the group of the user who started it. It is normally considered extremely bad practice to run a program in this way as it can pose many security problems.

    Latest versions of the Linux kernel will even prohibit the running of shell scripts that have SGID/SUID attribute set.

    Use of the SUID bit on binaries (to run with root privileges, aka &8221;setuid bit&8221;) MUST be limited to those shown in
    the following list:

    /bin/ping
    /bin/su
    /usr/bin/at
    /usr/bin/chage
    /usr/bin/chfn
    /usr/bin/chsh
    /usr/bin/crontab
    /usr/bin/gpasswd
    /usr/bin/newgrp
    /usr/bin/passwd


    The other binaries that were installed with the SUID bit set MUST have this bit removed. Administrators can still run
    these binaries normally, but they are not available for ordinary users. There are also a number of SGID files on the system that are needed, it may depending on the number of tools, or your distribution. Use Google and query the web for the right list ;-)

    Similarly, the SGID bit MUST NOT be used to give group &8221;root&8221; privileges to any binary.
    To generate a list of all SUID/SGID programs on the system simply run the following command:

    # find / -not -fstype ext3 -prune -o \ -type f \( -perm -4000 -o -perm -2000 \) \ -print


    Then, for each file in this list that is not one of the permitted SUID or SGID programs, run the command
    # chmod -s FILE

    to remove the SUID and SGID bits. When done, re-run the find command to verify that the list matches the
    permitted programs.

    I recommend you installing also FAF (File Anomaly Finder) on your server to check periodically for file with too much rights or privileges

  • if you encounter this error while starting oracle

    /etc/init.d/oraemctl start Starting Oracle EM DB Console:
    Environment variable ORACLE_UNQNAME not defined. 
    Please set ORACLE_UNQNAME to database unique name. OK
    One possible solution is to edit the script
    # vi /etc/init.d/oraemctl

    and add the following code in blue

    #!/bin/bash
    #
    # oraemctl Starting and stopping Oracle Enterprise Manager Database Control.
    # Script is valid for 10g and 11g versions.
    #
    # chkconfig: 35 80 30
    # description: Enterprise Manager DB Control startup script
    
    # Source function library.
    
    . /etc/rc.d/init.d/functions
    
    ORACLE_OWNER="oracle"
    ORACLE_HOME="/opt/oracle/otk/home"
    ORACLE_HOME="/opt/oracle/otk/home/app/oracle/product/11.2.0/dbhome_1"
    ORACLE_SID=orcl
    
    case "$1" in
     start)
      echo -n $"Starting Oracle EM DB Console:"
      su - $ORACLE_OWNER -c "ORACLE_SID=$ORACLE_SID $ORACLE_HOME/bin/emctl start dbconsole"
      echo "OK"
      ;;
     stop)
      echo -n $"Stopping Oracle EM DB Console:"
      su - $ORACLE_OWNER -c "ORACLE_SID=$ORACLE_SID $ORACLE_HOME/bin/emctl stop dbconsole"
      echo "OK"
      ;;
     *)
     echo $"Usage: $0 {start|stop}"
    ;;
    esac
    ~

    Use of course the right SID, after that Oracle Enterprise manager will start correctly

    # /etc/init.d/oraemctl start
    Starting Oracle EM DB Console:Oracle Enterprise Manager 11g Database Control Release 11.2.0.1.0
    Copyright (c) 1996, 2009 Oracle Corporation.&160; All rights reserved.
    - An instance of Oracle Enterprise Manager 11g Database Control is already running.
    OK

  • tatice-linux-tux-10409

    Some useful Bash Linux alias taken from my user profile. If you have a long command that you type frequently consider putting it in as an alias.

    In computing, alias is a command in various command line interpreters (shells) such as Unix shells, 4DOS/4NT and Windows PowerShell, which enables a replacement of a word with another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. [WikiPedia]

    Description &160;
    Find all directories and and chmod them to rwxr.xr.x alias fixpermD='find . -type d -exec chmod 755 {} \;'
    Find all files and and chmod them to rw.r..r.. alias fixpermF='find . -type f -exec chmod 644 {} \;'
    Both above and set recursively user and user group in one shot alias fixUserAPerms='fixpermF; fixpermD; chown -R userA .;chgrp -R usergrp .'
    Make a directory and all files recursively read only, secure but a pain to maintain. see next&160; alias ro='find . -type f -exec chmod 444 {} \;find . -type d -exec chmod 555 {} \;'
    Make a directory and all files recursively read write, just the time to update your site. alias rw='find . -type f -exec chmod 644 {} \;find . -type d -exec chmod 755 {} \;'
    Lower case all files in current directory alias lowercaseallfiles='for f in *; do mv $f `echo $f | tr [:upper:] [:lower:]`; done'
    List all open connections to your server alias listOpenConnections='lsof –i'
    List all internet connections alias listinternetconnection='netstat –lptu'
    find the 10 biggest in size directories alias dirsizes=’du -cks * | sort -n | tail –10'
    Show open port alias openports='netstat -nape --inet'
  • tatice-linux-tux-10409

    For day to day hacker, they may be more elegant ways to do it (PERL, AWK), but they have work well for me.

    • Upper case file name or variable,
    • Search and replace in all files content recursively,
    • Renaming all files recursively,
    • Renaming all directory recursively,
    • Copying a set of files from one directory to the other and preserving hierarchy.

    Upper case file name or variable

    function toUpper {
    echo "$1" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'&160;&160;
    # or
    # echo "$1" | tr -s&160; '[:lower:]'&160; '[:upper:]'
    }

    Usage:

    • in a script toUpper “cedric”
    • assign to a variable in a script result=$(toUpper $variable) or result=$(toUpper “cedric”)

    Search and replace in all files content recursively

    This can go through all directories

    • $3 as base directory
    • Search for $1
    • Replace with $2

    function searchAndReplaceRecursively {
    echo "replace '$1' with '$2' in dir '$3'";
    for file in `find $3 -type f`;
    do
    echo -ne ".";
    sed -e 's/'$1'/'$2'/g' $file > /tmp/tempfile.tmp
    mv /tmp/tempfile.tmp $file
    done
    echo "";
    }

    Usage:

    • in a script searchAndReplaceRecursively “cedric” “unix” .

    Renaming all files recursively

    This can go through all directories, renaming all files based on parameters:

    • $3 as base directory,
    • Search for $1 in filename,
    • Replace with $2 in filename.

    function renameFilenameRecursively {
    for file in `find $3 -type f`;
    do
    newFilename=`basename $file | sed -e 's/'$1'/'$2'/g'`;
    dirs=`dirname $file`;
    mv $file $dirs/$newFilename;
    done
    }

    Usage:

    • in a script renameFilenameRecursively “cedric” “unix” .

    Renaming all directory recursively

    This can go through all directories, renaming all directories based on parameters:

    • $3 as base directory
    • Search for $1 in directory name
    • Replace with $2 in directory name
    • in bold, you can put either
      • *$1 the pattern has to be at end of directory name,
      • $1* the pattern has to be at beginning of directory name,
      • *$1* the pattern has to be in the middle of directory name,
      • remove the case all together, the grep $1 filter already the result of the command find.

    function renameDirectoriesRecursively {
    for adir in `find $3 -type d | grep $1`;
    do
    case $adir in
    *$1* )
    newdirname=$( echo "$adir" | sed -e 's/'$1'/'$2'/g' );
    echo Found $adir will be renamed to $newdirname;
    mv $adir $newDirName;
    esac
    done
    }

    Usage:

    • in a script renameDirectoriesRecursively “cedric” “unix” .

    Copying a set of files from one directory to the other and preserving hierarchy

    # find . -name $1 -exec cp -r --parents {} $destination \;

  • joomla_cms

    Digital watermarking is the process of possibly irreversibly embedding information into a digital signal. The signal may be audio, pictures or video, for example. If the signal is copied, then the information is also carried in the copy. In visible watermarking, the information is visible in the picture or video. Typically, the information is text or a logo which identifies the owner of the media.[Wikipedia]

     

    I decide to go with an offline watermarking first, but you can let any Joomla!® extension watermark picture on the fly using php and .htaccess (at the cost of additional CPU server resources).

     

    Why Watermarking pictures?

    There is a lot of reasons, among others:

    • To limit images stealing, advanced users will still be able to crop/blur your watermark through!
    • To drive more new users/returning visitors to your site, anybody using your images in a forums may want to visit your site if the watermark can be read (don’t use complex logo, except if you have a well known brand)
    • To advertise your online work,

    Attention

    The first thing to do is to make a backup of your images! applying a watermarking is a non reversible process, usually this mean for Joomla!® to save your /images/stories directory

    You’ll have to decide what size you want your photos to be displayed at and how much space the text or logo will take up. Keep it small enough so as not to ruin the photo.

    Create a watermark using GIMP

    Download the excellent GIMP if not already done.

    Create a new picture with transparent background

    watermark.in.gimp.02 

    Using the text tool, create a black or white title, you can also import/create an original logo.

    watermark.in.gimp

    Save the file using a .png or .gif extension, don’t use .jpg as it don’t handle transparency

    Result:

    watermark 

    Prepare your Linux server

    My script require you to install ImageMagick

    ImageMagick®  is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

    ImageMagick install in OpenSuse

    As root run

    # zipper in ImageMagick

    ImageMagick install in Debian

    As root run

    # apt-get install ImageMagick

    Features

    Support dir name and filename space

    Support space in path and filename, not so easy as you think. Bash force me to use “” around variables to support space in path and use a while loop instead of a for loop to support space in filename.

    Scan you directories

    Scan your directories recursively for all pictures starting starting from the current directory (.).

    Do not watermark all pictures

    I don’t want to apply a watermark to all my pictures, I have decided to check against the size of the images and apply only if a threshold is reach (here all images bigger in width than 1024 pixels). The command identify allow you to retrieve the characteristics of any pictures

    geometry=$(identify $each)

    return for example

    ./desktop.jpg JPEG 1024x819 1024x819+0+0 DirectClass 8-bit 94.8613kb

    Add a bit of Bash REGEX magic (since 2004 with bash 4.0) to correctly identify picture size

    regex="([0-9]*)x([0-9]*)"

    and voila! I can now use $height and $width in my function.

    Limitations

    Do not run the script twice or more

    Do not run the script twice or more on same directory/sub directories, or the watermark will get darker and darker. The code do not check if any watermark already exist, and apply the same watermark over and over pictures that reach the threshold.

    Installation

    The script must be runnable

    # chmod u+x watermark.sh

    Configuration

    open the file watermark.sh

    # vi watermark.sh

    and change all variables value at the top

    Position of the Text Watermark

    You can position the text using the following Gravity, possible values are: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast.

    NorthWest North NorthEast
         
      Center  
         
    SouthWest South SouthEast

    If the -gravity option is present with NorthEast, East, or SouthEast gravity, it gives the distance leftward from the right edge of the image to the right edge of the cropping region. Similarly, if the -gravity option is present with SouthWest, South, or SouthEast gravity, the distance is measured upward between the bottom edges.

    See more details HERE  

    The bash script

    Source Code

     

     

    #!/bin/bash
    echo "*******************************************"
    echo "* Image Watermarking Script               *"
    echo "* By Cedric Walter - www.waltercedric.com *"
    echo "* Licence GNU/GPL v3 or later              *"
    echo "*******************************************"
    echo " "
    
    BASEDIR=.
    
    WATERMARK="/srv/www/vhosts/waltercedric.com/httpdocs/watermark.png"
    
    Below this width no watermark will be apllied
    MIN_PIC_HEIGHT=600
    MIN_PIC_WIDTH=800
    
    # NorthEast, East, or SouthEast gravity, it gives the distance leftward from the
    # right edge of the image to the right edge of the cropping region. # Similarly, if the -gravity option is present with SouthWest, South, or SouthEast
    # gravity, the distance is measured upward between the bottom edges. WATERMARK_POSITION=northeast Transparency percentage TRANSPARENCY_PERCENTAGE=15 normal user dont change below #this use bash regular expression capabilities regex="([0-9]*)x([0-9]*)" function applyWatermarkForExtension { find $BASEDIR -type f -name "$1" | while read each do geometry=$(identify "$each") echo $geometry if [[ ${geometry} =~ ${regex} ]]; then echo "$geometry matches" i=1 n=${BASH_REMATCH[*]} width=${BASH_REMATCH[$i]} let i++ height=${BASH_REMATCH[$i]} echo width=$width height=$height if [ $width -gt $MIN_PIC_WIDTH ];then echo "Working on $each..." composite -gravity $WATERMARK_POSITION -dissolve $TRANSPARENCY_PERCENTAGE $WATERMARK "$each" "$each" 2> /dev/null echo "... Done!" fi fi done } add new pictures extensions here applyWatermarkForExtension "*.jpg" applyWatermarkForExtension "*.gif" applyWatermarkForExtension "*.png" applyWatermarkForExtension "*.jpeg" exit 0

    Download

    Download from my download section.

    references