LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > baig
User Name
Password

Notices


Rate this Entry

My Journy of Shell Scripting

Posted 12-02-2008 at 03:37 AM by baig
Updated 12-03-2008 at 03:23 PM by baig

This is my first blog ever to be published. Now and own wards I would be simply posting my self written scripts for your comments.. Because, without your kind correction It would not be possible to write effective scripts..


Here is my first script

Description:
Searches for files


#! /bin/bash

clear
echo "============================="
echo "=========SEARCHTOOL=========="
echo "============================="
echo "Type name(HINT) of file:"
read nfile
echo ""
echo "Type your path to search"
read path
echo ""
echo "Type extension type of tile.e.g (.mp3, .txt, .rpm ): "
read kind

if [ -d $path ]; then
find $path -name "${nfile}*$kind"|sort
echo "Done!!!"

else

echo "$path is not a valid directory"
fi


#scripts ends
Posted in Uncategorized
Views 2157 Comments 4
« Prev     Main     Next »
Total Comments 4

Comments

  1. Old Comment

    easy Yam usage!!

    This is my second script!! I think it would be help full for those who don't want to end up in confusion using yum...

    In order to make download only work...you will have to download the plugin first...

    begin root or sudo. type

    Code:
    yum install yum-downloadonly.noarch -y
    For Newbies...

    Code:
    vim easyyum.sh
    #-----------copy from below------------

    #! /bin/bash

    clear

    echo "Type the starting name of package to list all avaialable: "
    read package


    yum list all | grep $package |sort|less


    echo "Copy and paste the required package(s) from above.. Paste below and hit enter: "
    read ins

    echo "Want to directly install(0) or download to directory(1):enter 1 or 0"
    read op;

    if [ $op -eq 1 ]; then
    echo "Enter directory to download: e.g (/root/, /home/user/ etc "
    read dir
    yum install $ins --downloadonly --downloaddir=$dir
    else
    yum install $ins
    fi

    echo "Done!!!"

    #---------------copy till here----------
    #Script ends..

    Now save and exit.. press esc twice and then type

    Code:
    :wq
    now change mode of script.. type

    Code:
    chmod 755 easyyum.sh
    you are done...

    now..

    Code:
    cd to_directory_where_easyyum.sh is
    then finally

    Code:
    sh easyyum.sh

    run it as root or sudo..

    Cheers!!
    Posted 12-02-2008 at 05:15 AM by baig baig is offline
    Updated 12-02-2008 at 06:45 AM by baig
  2. Old Comment

    Take an easy Backup!!

    Am absolutely free due to my university vacation and I'm loving spending hours and hours learning Linux... So what I learned to day so far is:

    Same steps mentioned above in previous post, to make it work. Copy paste to vim, change mode and execute..

    Description:

    Takes back of specified path to a specific directory.

    Reports completion status to logfile..

    --------------copy form below-------------


    #! /bin/bash

    clear
    echo "================================================================================"
    echo "===============================Back Up Utility=================================="
    echo "================================================================================"

    echo "Enter name of back up Date/month/year will be added automaticaly: "
    read name

    echo "Enter path to take backup of: "
    read take

    if [ -d $take ];then
    echo "Enter path to save backup in: "
    read save
    else
    echo "$take is nota a valid Directory: usage: e.g: /root, /boot e.t.c "
    echo "Back up of directory *** $take *** Failed at `date +%d-%m-%y-------%r`">>backuplog.log
    echo "">>backuplog.log
    echo "">>backuplog.log
    echo "">>backuplog.log
    exit 1
    fi


    tar -zcf $save/"${name}_`date +%d-%m-%y`.tgz" $take

    if [ $? -eq 0 ]
    then
    echo "Backup of **** $take *** was taken Successfully to Dir:$save">>backuplog.log
    echo "">>backuplog.log
    echo "">>backuplog.log
    fi


    -----------script ends-------------------

    you can change the logging path to your taste..

    Love Linux, Live Linux

    Cheers!!
    Posted 12-03-2008 at 03:14 PM by baig baig is offline
    Updated 12-03-2008 at 03:16 PM by baig
  3. Old Comment
    I added some usable features to the previous script.
    Support now also main idea of *nix:
    - software use stdin+stdout+stderr, caller can change
    it
    You can call xxx.sh -s from/here -d to/location -n usingname

    More error checking, exit if error

    -----------------------------------------------
    Quote:
    #!/bin/bash
    # this script is posix-sh compatible, you can use ksh,bash,posix-sh, ....
    # chmod a+rx this.sh
    # ./this.sh

    # save script name
    PRG=$0
    # ksh/bash/posix compatible echo no newline version
    ASK="echo -e -n"
    # logfile
    logf="backuplog.log"
    # init variable
    name=""
    save=""
    take=""

    ########################### function ###########################
    usage()
    {
    cat <<-EOT >&2
    usage:$PRG -s dir -d dir -n name
    EOT
    [ $# -gt 0 ] && exit $*
    }

    ############################ main ##############################
    # - look cmdline options
    while [ $# -gt 0 ]
    do
    flag="$1"
    case "$flag" in
    -s) # source dir
    take="$2"
    shift
    ;;
    -d) # destdir
    save="$2"
    shift
    ;;
    -n) # name
    name="$2"
    shift
    ;;
    --) break ;; # - no more options
    -*) usage 99 ;;
    *) # no arguments, so error
    usage 99
    ;;
    esac
    shift
    done

    # HERE is nice "template" method
    cat <<EOT
    ================================================================================
    ===============================Back Up Utility==================================
    ================================================================================
    $(date '+%Y-%m-%d') == $(hostname) == $(id -u -n) ==
    $take -> $save ($name)
    EOT

    if [ "$name" = "" ] ; then
    $ASK "Enter name of back up Date/month/year will be added automaticaly: "
    read name
    fi
    [ "$name" = "" ] && exit 2

    if [ "$take" = "" ] ; then
    $ASK "Enter path to take backup of: "
    read take
    fi
    [ "$take" = "" ] && exit 3

    if [ ! -d "$take" ] ; then # - dir ?
    # error redirect to the stderr
    echo "$take is not a valid Directory: usage: e.g: /root, /boot e.t.c " >&2
    cat <<-EOT >> $logf
    Back up of directory *** $take *** Failed at $(date '+%Y-%m-%d-------%r')


    EOT
    exit 1
    fi

    if [ "$save" = "" ] ; then
    $ASK "Enter path to save backup in: "
    read save
    fi
    [ "$save" = "" ] && exit 4

    mkdir "$save" >/dev/null 2>&1 # -make if needed

    if [ ! -d "$save" ] ; then # - dir ?
    echo "no $save dir ???" >&2
    exit 5
    fi

    # using ex. variables give more readable code
    bfile="$save/${name}_$(date '+%Y%m%d.%H%M%S').tgz"

    if tar -zcf "$bfile" "$take"
    then
    cat <<-EOT >> $logf
    Backup of **** $take *** was taken Successfully to Dir:$save


    EOT
    else
    echo "tar error" >&2
    fi
    Posted 12-04-2008 at 01:40 AM by kshfi kshfi is offline
  4. Old Comment

    A two lined script to keep you awair of an attack.!!

    @above:First of all thanks for you improved script.

    To control your computer over internet ssh is used secure shell, which lets a user/hacker to login to your computer by entering password. These events are logged into /var folder and its pretty hard for a beginner to sort it out..
    I know many of us don't know where to search for such logs to see who has attempt and when..

    The safest why, if you are sure you don't need ssh service ... stop it from services.

    To get daily report automatically to your specified directory follow the steps:
    logdir is Fedora default log route
    Code:
    mkdir /root/SSH_attempts
    Code:
    vim checkssh.sh
    ***once you copy paste the code from below****
    chmod 755 checkssh.sh
    ######################################
    ######FOR BEGINNERS###################
    ######################################

    #!/bin/sh

    cat /var/log/secure | grep "Failed password"|sort -n>/root/SSH_attempts/sshfailattempts.log
    cat /var/log/secure | grep "Accepted password"|sort -n>/root/SSH_attempts/sshsuccessattempts.log


    save this script under root

    Start you cron service if not started.

    type

    Code:
    crontab -e
    1 * * * * /root/checkssh.sh

    save and exit..

    now you will have update to your folder SSH_attempts
    with two logfiles..

    one for failed attempts.
    send for success attempts.

    Cheers!!
    Posted 12-04-2008 at 06:44 AM by baig baig is offline
    Updated 12-04-2008 at 06:45 AM by baig
 

  



All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration