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...

79 Responses

  1. Gulab Singh says:

    can you help me .
    how to use counter variable in shell script ?

  2. Egeni says:

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

  3. 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?

  4. lee says:

    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?

  5. Marin Todorov says:

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

    bld=$’ \e[1m’

    Afte that:

    echo $red text $end

    “text” will be covered in red

  6. 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.

  7. Teo says:

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

  8. 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:


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

      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
      echo “memeory usage high, cleaning cache”
      sh -c “sync; echo 1 > /proc/sys/vm/drop_caches”

      • Ravi Saive says:


        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}’

          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}’

          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
          echo “memeory usage high, cleaning cache”
          sh -c “sync; echo 1 > /proc/sys/vm/drop_caches”

          • Ravi Saive says:


            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..

  9. NJ says:

    thanks a mile :)

  10. 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