Shell Scripting FAQ

download Shell Scripting FAQ

of 4

Transcript of Shell Scripting FAQ

  • 8/7/2019 Shell Scripting FAQ

    1/4

    In shell scripting How to indentify that the previous command was run successfully?

    How will you write a shell script to connect to SQL database?

    What does UID and GID signify?

    What are the different security mechanisms available in UNIX?

    What are the different types of shells available in UNIX?

    how many users have logged in and logged out in last five or 10 minutes

    How do you search the string for vowel's occurrence and number of occurrences of

    each vowel

    What is make used for? How is it different from a shell script?

    How to compare floating point number in shell scripting ?

    How to delete a word from a file using shell scripting???

    How to extract the second row of a text-file?

    How to compare two floating point numbers ?

    How to compress files by using shell scripting

    What are the steps to take files from unix server to windows?

    How to find see the file which is created today,s date with time after 10 a.m to 5

    p.m?

    What is the basic difference u find between a shell script and perl.I mean the

    advantages of one over

    What is use of "cut" command ?Give some examples. Can we use "awk" or "sed"

    How Connect to a Database in Shell Programming?Please tell me Step by Step?

    What is this line in the shell script do #!/bin/ksh?

    Write a shell script to identify the given string is palindrome or not?

    What is the difference between writing code in shell and editor?

    What is INODE?

    What is the difference between a 'thread' and a 'process'?

    What does $# stand for?

  • 8/7/2019 Shell Scripting FAQ

    2/4

    What is $*?

    If you have a string "one two three", Which shell command would you use to extract

    the strings?

    What is the difference between a shell variable that is exported and the one that is

    not exported?

    How will you list only the empty lines in a file (using grep)?

    How do you schedule a command to run at 4:00 every morning?

    How do u open a read only file in Unix?

    What are the different kinds of loops available in shell script?

    *************************************************************************************

    **********************

    How do you find out whats your shell? - echo $SHELL

    Whats the command to find out todays date? - date

    Whats the command to find out users on the system? - who

    How do you find out the current directory youre in? - pwd

    How do you remove a file? - rm

    How do you remove a - rm -rf

    How do you find out your own username? - whoami

    How do you send a mail message to somebody? - mail

    [email protected] -s Your subject -c [email protected]

    How do you count words, lines and characters in a file? - wc

    How do you search for a string inside a given file? - grep string filename

    How do you search for a string inside a directory? - grep string * (linux) other use -find / |xargs grep string

    How do you search for a string in a directory with the subdirectories recursed? -

    grep -r string *

    What are PIDs? - They are process IDs given to processes. A PID can vary from 0 to

    65535.

  • 8/7/2019 Shell Scripting FAQ

    3/4

    How do you list currently running process? - ps

    How do you stop a process? - kill pid

    How do you find out about all running processes? - ps -ag

    How do you stop all the processes, except the shell window? - kill 0

    How do you fire a process in the background? - ./process-name &

    How do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is

    your script name.

    Whats the conditional statement in shell scripting? - if {condition} then fi

    How do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge

    How do you test for file properties in shell scripts? - -s filename tells you if the file is

    not empty, -f filename tells you whether the argument is a file, and not a directory,-d filename tests if the argument is a directory, and not a file, -w filename tests for

    writeability, -r filename tests for readability, -x filename tests for executability

    How do you do Boolean logic operators in shell scripting? - ! tests for logical not, -a

    tests for logical and, and -o tests for logical or.

    How do you find out the number of arguments passed to the shell script? - $#

    Whats a way to do multilevel if-elses in shell scripting? - if {condition} then

    {statement} elif {condition} {statement} fi

    How do you write a for loop in shell? - for {variable name} in {list} do {statement}

    done

    How do you write a while loop in shell? - while {condition} do {statement} done

    How does a case statement look in shell scripts? - case {variable} in {possible-

    value-1}) {statement};; {possible-value-2}) {statement};; esac

    How do you read keyboard input in shell scripts? - read {variable-name}

    How do you define a function in a shell script? - function-name() { #some code here

    return }

    How does getopts command work? - The parameters to your script can be passed

    as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while

    getopts n:x option, and the variable $option contains the value of the entered

    option.

    **********************************************************************************

  • 8/7/2019 Shell Scripting FAQ

    4/4

    What is this line in the shell script do ??? #!/bin/ksh

    This line is called as "Hash Bang" Statement. This tells the OS that the particular

    needs the respective shell for execution. If a script file has this hash bang statement

    along with execution permission, then this file can be run directly without invoking

    thru shell command.Ex:$ instead of$kshHow would you using the commands ps, cut, tr and kill, along with pipes, write a

    command that will find all sleep processes running on the system and kill them?

    for i in `ps -eaf | tr -s ' ' | awk '{ print $2 }'`

    do

    kill -9 $i

    done