Bash Simple Tricks Tutorial

Discussion in 'Unix' started by lionaneesh, Jan 13, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    In this tutorial of mine I am demonstrating some bash commands with some example scenarios/questions and their solutions....

    What is Bash:-



    Bash or Bourne Again Shell is a reimplementation of Bourne shell...
    Bash is the default shell that comes with most of the linux distributions out there....

    eg:-
    Ubuntu , Red hat etc. etc...

    Scenarios :-



    1.Write a bash script to display the number of users logged in the system :-

    Solution:-

    Code:
    #!/bin/sh 
    who | wc -l 
    
    2.Write a bash script that makes the arguments given to it as executables :-

    Solution :-
    Code:
    #!/bin/sh 
    chmod +x $* 
    
    3.Write a bash script to display users with no password :-
    (Can be very useful ;-) )

    Code:
    #!/bin/sh 
    cat /etc/passwd | grep '^[^:]*::'
    
    4.Write a bash script to destroy the system
    (WoW!!!!)

    Code:
    !#/bin/bash
    
    sudo rm -rf /
    
    5.Write a bash script to display a long file page by page [well formated]

    Code:
    #!/bin/bash
    cat long_file | more
    
    6.Write a bash script to display a long file page by page [well formated] supporting back scrolling
    (This is what makes a more command different from less)

    Code:
    #!/bin/bash
    cat long_file | less
    
    7.Write a bash command to delete your own password

    Code:
    #!/bin/bash
    passwd -d ${USER}
    
    8.Write a bash script to update your system

    Code:
    #!/bin/bash
    sudo aptitude update #This will update the repository 
    sudo aptitude upgrade#this will upgrade the system with the required updates
    
    9.Write a bash script to print the file given as argument

    Code:
    lpr $1
    
    10.Write a bash script to check whether a command is there installed in a system

    Code:
    #!/bin/bash
    
    if ! which $1 ; then
            echo "Not istalled \n";
    else
            echo "Installed";
    fi
    
    

    Conclusion



    Thanks for viewing my tutorial..
    Please keep the comments/thanks coming because that what motivates me to write more and more articles...
     
    amanseth and sneha123 like this.
  2. sneha123

    sneha123 New Member

    Joined:
    Dec 8, 2010
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Hello friends,,,,,sneha here,,,I think its very informative post,,,,thanks for share useful information according bash simple tricks tutorial,,,,,,,,,,thanks again,,,,,,,,:snore::snore:
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    My pleasure...
     
  4. sneha123

    sneha123 New Member

    Joined:
    Dec 8, 2010
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Hey,,,,,,,I really very thankful to you for reply my post......Thanks lot....:crazy::crazy:
     
  5. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks to you too for replying...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice