5 Shell Scripts for Linux Newbies to Learn Shell Programming – Part II

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux course in 5 Days

We are thankful for your never ending support.

Receive Your Free Complimentary eBook NOW! -

Download Free Linux eBooks

Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Introduction to Linux - A Hands on Guide

You may also like...

78 Responses

  1. Egeni says:

    Pls try and explain each script in details and can u give me the code to decrypt an encrypted script. Thanks guys

  2. Hieu Dinh says:

    Can you hep me to write a bash script that change the passphrase in a *.gpg file which i created before. The situation is that I create a decrypted file with passphrase by follow you above. Now i want to change the passphrase of that decrypted file, is that possible?

  3. lee says:

    Hello
    I have tried the script1, I got error “syntax error: unexpected end of file”. I search google, but still not be able fix it.
    Can you please advise?

  4. Marin Todorov says:

    I think for defining colors, it would be much easier if you do:

    red=$’\e[1;31m’
    grn=$’\e[1;32m’
    yel=$’\e[1;33m’
    blu=$’\e[1;34m’
    mag=$’\e[1;35m’
    cyn=$’\e[1;36m’
    bld=$’ \e[1m’
    end=$’\e[0m’

    Afte that:

    echo $red text $end

    “text” will be covered in red

  5. vishwa says:

    Hi Avishek,
    Drawing a Special Pattern: Throwing an error
    ./pattern.sh: 19: ./pattern.sh: Syntax error: Bad for loop variable

    I have given 777 permission of the file.

  6. Teo says:

    You’re great man, thanks! short and precise!

  7. Atul Patil says:

    HI Avishek,

    Nice blog , can you please guide me how to create one scripts it will check first memory utilazation if it is high then run command sh -c “sync; echo 1 > /proc/sys/vm/drop_caches”

    • paresh says:

      Hi,

      I am new to shell scripting, i hope below script helps

      #!/bin/bash
      limit=100 #minimum free memory you need
      free=`free -m | awk ‘{print $3}’ | xargs | awk ‘{print $2}’`

      if [ $free -ge $limit ]; then
      echo “memory is okay”
      exit 1
      fi
      echo “memeory usage high, cleaning cache”
      sh -c “sync; echo 1 > /proc/sys/vm/drop_caches”

      • Ravi Saive says:

        @Paresh,

        Thanks for the script, but you should tell us what this script all about, so that other users can aware of it..

        • paresh says:

          @Ravi: my bad :)

          We need free ram available on server, for that we can use free -m command:
          [root@srv ~]# free -m
          total used free shared buffers cached
          Mem: 996 579 417 0 8 171
          -/+ buffers/cache: 398 597
          Swap: 2015 0 2015

          now we will try to print only free ram cloumn:

          [root@srv ~]# free -m | awk ‘{print $3}’
          free
          579
          398
          0

          using xargs, we can get this data in single line and print only 2nd field:

          [root@srv ~]# free -m | awk ‘{print $3}’ | xargs
          free 579 399 0

          [root@srv ~]# free -m | awk ‘{print $3}’ | xargs | awk ‘{print $2}’
          579

          now we will store this value in free variable:
          free=`free -m | awk ‘{print $3}’ | xargs | awk ‘{print $2}’`

          you can define a variable “limit”, which will be the minimum amount of ram you want free in system.

          then check if free variable is greater than or equal to limit, if yes then everything is okay else clear cache:
          if [ $free -ge $limit ]; then
          echo “memory is okay”
          exit 1
          fi
          echo “memeory usage high, cleaning cache”
          sh -c “sync; echo 1 > /proc/sys/vm/drop_caches”

          • Ravi Saive says:

            @Paresh,

            Good nice script for checking free memory usage on Linux, but you’ve tested this script on all Linux distributions? If it works perfectly on all Linux OS, why not create a small 300 words article on this and publish at Tecmint?

            If you planning to write a article on this, here are the guidelines to follow:

            Introduce yourself, about script and explain each parameter used in this script and place the script in the article and show real time testing of scripts with screenshot and at the end add conclusion about script..

  8. NJ says:

    thanks a mile :)

  9. Prince says:

    How to print a time stamp of multiple files from different path?

    • Avishek Kumar says:

      How do you get time stamp of one file?
      simply by executing of the below commans
      $ stat /path/to/file/file_name
      $ ls -l /path/to/file/file_name
      $ ls -lc /path/to/file/file_name
      $ ls -lu /path/to/file/file_name

      So how will you print time stamp of more than one file from different location
      $ stat /path/to/file/file_name /path/to/file2/file_name2
      $ ls -l /path/to/file/file_name /path/to/file2/file_name2
      $ ls -lc /path/to/file/file_name /path/to/file2/file_name2
      $ ls -lu /path/to/file/file_name /path/to/file2/file_name2

      Hope it helps!

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

Join Over 150K+ Linux Users
  1. 100,756
  2. 5,113
  3. 36,418

Enter your email to get latest Linux Howto's