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

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

89 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:
          [[email protected] ~]# 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:

          [[email protected] ~]# free -m | awk ‘{print $3}’

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

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

          [[email protected] ~]# 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.