Linux Inbuild self-help commands (help or man or info)

Linux has many commands available which can be very useful. Even it is very powerful when it gets all the available parameters along with the commands. Due to the varsity of the commands available in the Linux, it’s difficult to remember all the commands.

How to use –h or –help?

Launch the terminal by pressing Ctrl+ Alt+ T or just click on the terminal icon in the taskbar. Simply type your command whose usage you to know in the terminal with –h or –help after space and press enter. And you’ll get the complete usage of that command as shown below.

Description

help displays brief summaries of shell builtin commands. If PATTERN is specified, gives detailed help on all commands matching PATTERN, otherwise the list of help topics is printed.

Syntax

help [-dms] [PATTERN ...]

Options

-dOutput short description for each topic.
-mDisplay usage in pseudo-manpage format.
-sOutput only a short usage synopsis for each topic matching.

Examples

help echo

Display a brief description of the builtin shell command echo.

Linux man command

The “man” is a short term for manual page. In unix like operating systems such as linux, man is an interface to view the system’s reference manual.

A user can request to display a man page by simply typing man followed by a space and then argument. Here its argument can be a command, utility or function. A manual page associated with each of these arguments is displayed.

If you will provide a section number in the command, then man will be directed to look into that section number of the manual and that section page will be displayed. And if not, then by default it will display the first page and you have to go through the entire sections in a pre-defined manner.

We’ll read about section number in this tutorial.

Syntax of man:

  1. man [option(s)] keyword(s)  

But generally [option(s)] are not used. Only keyword is written as an argument.

For example,

  1. man ls  

This command will display all the information about ‘ls’ command as shown in the screenshot.

info command in Linux with Examples

infocommand reads documentation in the info format. It will give detailed information for a command when compared with the man page. The pages are made using the texinfo tools because of which it can link with other pages, create menus and easy navigation.

Syntax:

info [OPTION]... [MENU-ITEM...]

Options:

  • -a, –all: It use all matching manuals.
  • -k, –apropos=STRING: It look up STRING in all indices of all manuals.
  • -d, –directory=DIR: It add DIR to INFOPATH.
  • -f, –file=MANUAL: It specify Info manual to visit.
  • -h, –help: It display this help and exit.
  • -n, –node=NODENAME: It specify nodes in first visited Info file.
  • -o, –output=FILE: It output selected nodes to FILE.
  • -O, –show-options, –usage: It go to command-line options node.
  • -v, –variable VAR=VALUE: It assign VALUE to Info variable VAR.
  • –version: It display version information and exit.
  • -w, –where, –location: It print physical location of Info file.

Examples:

  • -a : It use all matching manuals and displays them for a particular command. info -a cvs

Shell Scripting Conditional Statement

Conditional Statements: There are total 5 conditional statements available in the bash programming,Which can be implemented in the different situation in the bash/shell programming.

  1. if statement
  2. if-else statement
  3. if..elif..else..fi statement (Else If ladder)
  4. if..then..else..if..then..fi..fi..(Nested if)
  5. switch statement

if statement

If statement is the simplest statement in the series.Where it is used to compare the two variable. This block will process if specified condition is true.
Syntax:

if [ expression ]
then
   statement
fi

if-else statement

If specified condition is not true in if part then else part will be execute.This can be helpful when we have 3 variables and need to compare between
Syntax

if [ expression ]
then
   statement1
else
   statement2
fi

if..elif..else..fi statement (Else If ladder)

We can use to compare more than 3 variables by using this block.To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the condition is true then it processes else part.
Syntax

if [ expression1 ]
then
   statement1
   statement2
   .
elif [ expression2 ]
then
   statement3
   statement4
   .
else
   statement5
fi

if..then..else..if..then..fi..fi..(Nested if)

Generally Nested block is used for comparison of multiple nested conditions. if-else block can be used when, one condition is satisfies then it again checks another condition. In the syntax, if expression1 is false then it processes else part, and again expression2 will be check.
Syntax:

if [ expression1 ]
then
   statement1
   statement2
   .
else
   if [ expression2 ]
   then
      statement3
      .
   fi
fi

switch statement

case statement works as a switch statement if specified value match with the pattern then it will execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.

Syntax:

case  in
   Pattern 1) Statement 1;;
   Pattern n) Statement n;;
esac

Shell Scripting Functionality

Whatever you can do with shell script, you can do with perl and python too or in general any scripting language. However These languages have advantage and disadvantage over each other.

Here are the few reasons why i use the scripts.

  • You can write a script to initialize something at boot time of the system. so you don’t need to do manually.
  • You can write a script which installs per-requisite and build the code with user input to enable/disable some features.
  • To kill or start multiple applications together.
  • To observe large database of files and find some patterns out of it.
  • System boot scripts (/etc/init.d)
  • System administrators, for automating many aspects of computer maintenance, user account creation etc.
  • Application package installation tools More detail
  • Application startup scripts, especially unattended applications (e.g. started from cron or at)
  • Any user needing to automate the process of setting up and running commercial applications, or their own code.

Unix / Linux – Shell Basic Operators

There are various operators supported by each shell. We will discuss in detail about Bourne shell (default shell) in this chapter.According to the operation performed on the operators it has been classified into the below types.

We will now discuss the following operators −

  • Arithmetic Operators
  • Relational Operators
  • Boolean Operators
  • String Operators
  • File Test Operators

Bourne shell didn’t originally have any mechanism to perform simple arithmetic operations but it uses external programs, either awk or expr.

The following example shows how to add two numbers − 

#!/bin/sh

val=`expr 2 + 2`
echo "Total value : $val"

The above script will generate the following result −

Total value : 4

The following points need to be considered while adding −

  • There must be spaces between operators and expressions. For example, 2+2 is not correct; it should be written as 2 + 2.
  • The complete expression should be enclosed between ‘ ‘, called the backtick.

Arithmetic Operators

The following arithmetic operators are supported by Bourne Shell.

Assume variable a holds 10 and variable b holds 20 then −

OperatorDescriptionExample
+ (Addition)Adds values on either side of the operator`expr $a + $b` will give 30
– (Subtraction)Subtracts right hand operand from left hand operand`expr $a – $b` will give -10
* (Multiplication)Multiplies values on either side of the operator`expr $a \* $b` will give 200
/ (Division)Divides left hand operand by right hand operand`expr $b / $a` will give 2
% (Modulus)Divides left hand operand by right hand operand and returns remainder`expr $b % $a` will give 0
= (Assignment)Assigns right operand in left operanda = $b would assign value of b into a
== (Equality)Compares two numbers, if both are same then returns true.[ $a == $b ] would return false.
!= (Not Equality)Compares two numbers, if both are different then returns true.[ $a != $b ] would return true.

It is very important to understand that all the conditional expressions should be inside square braces with spaces around them, for example [ $a == $b ] is correct whereas, [$a==$b] is incorrect.

All the arithmetical calculations are done using long integers.

Relational Operators

Bourne Shell supports the following relational operators that are specific to numeric values. These operators do not work for string values unless their value is numeric.

For example, following operators will work to check a relation between 10 and 20 as well as in between “10” and “20” but not in between “ten” and “twenty”.

Assume variable a holds 10 and variable b holds 20 then −

OperatorDescriptionExample
-eqChecks if the value of two operands are equal or not; if yes, then the condition becomes true.[ $a -eq $b ] is not true.
-neChecks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.[ $a -ne $b ] is true.
-gtChecks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true.[ $a -gt $b ] is not true.
-ltChecks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true.[ $a -lt $b ] is true.
-geChecks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true.[ $a -ge $b ] is not true.
-leChecks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true.[ $a -le $b ] is true.

It is very important to understand that all the conditional expressions should be placed inside square braces with spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <= $b] is incorrect.

Boolean Operators

The following Boolean operators are supported by the Bourne Shell.

Assume variable a holds 10 and variable b holds 20 then −

OperatorDescriptionExample
!This is logical negation. This inverts a true condition into false and vice versa.[ ! false ] is true.
-oThis is logical OR. If one of the operands is true, then the condition becomes true.[ $a -lt 20 -o $b -gt 100 ] is true.
-aThis is logical AND. If both the operands are true, then the condition becomes true otherwise false.[ $a -lt 20 -a $b -gt 100 ] is false.

String Operators

The following string operators are supported by Bourne Shell.

Assume variable a holds “abc” and variable b holds “efg” then −

OperatorDescriptionExample
=Checks if the value of two operands are equal or not; if yes, then the condition becomes true.[ $a = $b ] is not true.
!=Checks if the value of two operands are equal or not; if values are not equal then the condition becomes true.[ $a != $b ] is true.
-zChecks if the given string operand size is zero; if it is zero length, then it returns true.[ -z $a ] is not true.
-nChecks if the given string operand size is non-zero; if it is nonzero length, then it returns true.[ -n $a ] is not false.
strChecks if str is not the empty string; if it is empty, then it returns false.[ $a ] is not false.

File Test Operators

We have a few operators that can be used to test various properties associated with a Unix file.

Assume a variable file holds an existing file name “test” the size of which is 100 bytes and has readwrite and execute permission on −

OperatorDescriptionExample
-b fileChecks if file is a block special file; if yes, then the condition becomes true.[ -b $file ] is false.
-c fileChecks if file is a character special file; if yes, then the condition becomes true.[ -c $file ] is false.
-d fileChecks if file is a directory; if yes, then the condition becomes true.[ -d $file ] is not true.
-f fileChecks if file is an ordinary file as opposed to a directory or special file; if yes, then the condition becomes true.[ -f $file ] is true.
-g fileChecks if file has its set group ID (SGID) bit set; if yes, then the condition becomes true.[ -g $file ] is false.
-k fileChecks if file has its sticky bit set; if yes, then the condition becomes true.[ -k $file ] is false.
-p fileChecks if file is a named pipe; if yes, then the condition becomes true.[ -p $file ] is false.
-t fileChecks if file descriptor is open and associated with a terminal; if yes, then the condition becomes true.[ -t $file ] is false.
-u fileChecks if file has its Set User ID (SUID) bit set; if yes, then the condition becomes true.[ -u $file ] is false.
-r fileChecks if file is readable; if yes, then the condition becomes true.[ -r $file ] is true.
-w fileChecks if file is writable; if yes, then the condition becomes true.[ -w $file ] is true.
-x fileChecks if file is executable; if yes, then the condition becomes true.[ -x $file ] is true.
-s fileChecks if file has size greater than 0; if yes, then condition becomes true.[ -s $file ] is true.
-e fileChecks if file exists; is true even if file is a directory but exists.[ -e $file ] is true.

Unix / Linux – Special Variables

In the last post we learned how to declare variables and how to naming conventions.Where we can only use the “_” underscore symbol in the variable name.Where excluding “_” symbol we can not able to use other symbol. This is because those characters are used in the names of special Unix variables. These variables are reserved for specific functions.

For example, the $ character represents the process ID number, or PID, of the current shell −

$echo $$

The above command writes the PID of the current shell −

29949

The following table shows a number of special variables that you can use in your shell scripts −

Sr.No.Variable & Operations
1$0 : The filename of the current script.
2$n : These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
3$# : The number of arguments supplied to a script.
4$* : All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
5[email protected] : All the arguments are individually double quoted. If a script receives two arguments, [email protected] is equivalent to $1 $2.
6$? : The exit status of the last command executed.
7$$ : The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
8$! : The process number of the last background command.

Command-Line Arguments

The command-line arguments $1, $2, $3, …$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, …$9 as the arguments to the command.

Following script uses various special variables related to the command line −

#!/bin/sh

echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: [email protected]"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

Here is a sample run for the above script −

$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2

Special Parameters $* and [email protected]

There are special parameters that allow accessing all the command-line arguments at once. $* and [email protected] both will act the same unless they are enclosed in double quotes, “”.

Both the parameters specify the command-line arguments. However, the “$*” special parameter takes the entire list as one argument with spaces between and the “[email protected]” special parameter takes the entire list and separates it into separate arguments.

We can write the shell script as shown below to process an unknown number of commandline arguments with either the $* or [email protected] special parameters −

#!/bin/sh

for TOKEN in $*
do
   echo $TOKEN
done

Here is a sample run for the above script −

$./test.sh Zara Ali 10 Years Old
Zara
Ali
10
Years
Old

Note − Here do…done is a kind of loop that will be covered in a subsequent tutorial.

Exit Status

The $? variable represents the exit status of the previous command.

Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.

Some commands return additional exit statuses for particular reasons. For example, some commands differentiate between kinds of errors and will return various exit values depending on the specific type of failure.

Following is the example of successful command −

$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2
$echo $?
0
$

Shell Scripting Variables Declaration

A variable is a character string where we stores a value. The value assigned could be a number, text, filename, device, or any other type of data.

A variable is nothing more than a pointer to the actual data stored. The shell enables you to create, assign, and delete variables.Their are some predefined convention as follows to define variables.

Variable Names

The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).

Unix shell variables should have their names in UPPERCASE.

Below are the examples are valid variable names −

_SAMPLE
RANDOM_A
V_1
VAR_2
VARIABLE_3

Following are the examples of invalid variable names −

2_TEMP  -   Starts With numeric
-VAR    -   Starts with &contains -
VARABLE1-VAR2    -   Contains -
VAR_A!  -    Contains !

Why a variable cannot use other characters such as !*, or  is that these characters have a special meaning for the shell?

The special character other than “_” is reversed in shell for other uses.

Defining Variables

Variables are defined with an = sing. The left part contains the name of the variable and right part contains the value of the variable.

Syntax   :   variable_name=variable_value

For example −

NAME="Zara Ali"

The above example defines the variable NAME and assigns the value “Zara Ali” to it. Variables of this type are called scalar variables. A scalar variable can hold only one value at a time.

Shell enables you to store any value you want in a variable. For example −

VAR1="Zara Ali"
VAR2=100

Accessing Values

To access the value stored in a variable, prefix its name with the dollar sign ($) −

For example, the following script will access the value of defined variable NAME and print it on STDOUT

#!/bin/sh

NAME="Zara Ali"
echo $NAME

The above script will produce the following value −

Zara Ali

Read-only Variables

Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed.

For example, the following script generates an error while trying to change the value of NAME −

#!/bin/sh NAME="Zara Ali" readonly NAME NAME="Qadiri"

The above script will generate the following result −

/bin/sh: NAME: This variable is read only.

Unsetting Variables

Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable.

Following is the syntax to unset a defined variable using the unset command −

unset variable_name

The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the command works −

#!/bin/sh

NAME="Zara Ali"
unset NAME
echo $NAME

The above example does not print anything. You cannot use the unset command to unset variables that are marked readonly.

Shell Scripting Basic | Understanding Linux Shell

Simply definition, the shell is a program that takes inputs as commands from the keyboard and gives them to the operating system to perform action . In the older days, it was the only way of user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell. In this post we discuss about the different terminology related to shell scripting.

  1. Shell:The shell scripting is a Command-Line Interpreter which connects a user to Operating System.By which the user execute the commands.Which has executed by the compiler.
  2. Process: Any task that a user run in the system is called a process. Which execute inside a processor with some protocol. A process is little more complex than just a task.
  3. File:A file is something which resides on hard disk (HDD) and contains data owned by a user.
  4. X-windows aka windows: A mode of Linux where screen (monitor) can be split in small “parts” called windows, that allow a user to do several things at the same time and/or switch from one task to another easily and view graphics in a nice way.
  5. Text terminal: A monitor that has only the capability of displaying text stuff in black and white, where no graphics or a very basic graphics display.
  6. Session: The period between logging on and logging out of the system.

Types of Shell on a Standard Linux Distribution

Bourne shell : The Bourne shell was one of the major shells used in early versions and became a de facto standard. It was written by Stephen Bourne at Bell Labs. Every Unix-like system has at least one shell compatible with the Bourne shell. The Bourne shell program name is “sh” and it is typically located in the file system hierarchy at /bin/sh.

C shell: The C shell was developed by Bill Joy for the Berkeley Software Distribution. Its syntax is modeled after the C programming language. It is used primarily for interactive terminal use, but less frequently for scripting and operating system control. C shell has many interactive commands.

Linux Shell Pathway for Automation

There exist thousands of commands for command-line user, how about remembering all of them? Hmmm! Simply you can not. The real power of computer is to ease the ease your work, you need to automate the process and hence you need scripts.

Scripts are collections of commands, stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard. The shell also provides a variety of useful programming features to make scripts truly powerful.

Advanced Linux Commands to Make You Expert User

Linux commands and their optional parameter are very useful for the user who wants to take advance of the full feature of Linux CLI. In this post, we have discussed the most featured commands are being used extensively in day-to-day activities.

To explore more about commands you can explore all the commands available in using the Linux Inbuild self-help commands (help or man or info) available, Which will give the briefing about the options and uses by precise examples.

A

alias


The alias command is a way to keep your command /commands in a temporary variable. Unix commands using a shorter name than those that are usually associated with such commands.

Syntax:
alias shortName="your custom command here"

Example:
alias wr=”cd /var/www/html”

apt-get

APT (Advanced Package Tool) is the command line tool to interact with this packaging system. There is already dpkg commands to manage it. But apt is more friendly way to handle packaging. You can use it to find and install new packages, upgrade packages, clean the packages etc.

Syntax:
sudo apt-get install <package_name>
sudo apt-get remove <package_name>
sudo apt-get purge <package_name>
sudo apt-get clean
sudo apt-get autoclean
sudo apt-get autoremove

Example: 
sudo apt-get install pinta

AWK, Gawk



awk command searches files for text containing a pattern. When a line or text matches, awk performs a specific action on that line/text. The Program statement tells awk what operation to do; Program statement consists of a series of “rules” where each rule specifies one pattern to search for, and one action to perform when a particular pattern is found. A regular expression enclosed in slashes (/) is an awk pattern to match every input record whose text belongs to that set.

Syntax:
awk 'pattern {action}' input-file > output-file

Example:
awk '{ print $5 }' table1.txt > output1.txt

bzip2


A portable, fast, open source program that compresses and decompresses files at a high rate, but that does not archive them.

Syntax: 
bzip2 option(s) filenames


Example:
bzip2 -z backup.tar

cat


A Unix/Linux command that can read, modify or concatenate text files. The cat command also displays file contents.

Syntax: 
cat [OPTION] [FILE]...


Example:
cat /etc/passwd
cat test test1

cd


The cd command changes the current directory in Linux and can conveniently toggle between directories. The Linux cd command is similar to the CD and CHDIR commands in MS-DOS.

Syntax:  
cd [-L | -P [-e]] directory


Example:
cd documents/work/accounting

Options
L
Force symbolic links to be followed. In other words, if you tell cd to move into a "directory", which is actually a symbolic link to a directory, it moves into the directory the symbolic link points to.
This option is the default behavior of cd; normally, it will always act as if -L has been specified.
-P
Use the physical directory structure without following symbolic links. In other words, only change into the specified directory if it actually exists as named; symbolic links will not be followed. This option is the opposite of the -L option, and if they are both specified, this option will be ignored.
-e
If the -P option is specified, and the current working directory cannot be determined, this option tells cd to exit with an error. If -P is not specified along with this option, this option has no function.

chmod


The chmod command changes the permissions of one or more files. Only the file owner or a privileged user can change the access mode.

Syntax:  
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...


[reference]
Reference Class Description
u owner file's owner
g group users who are members of the file's group
o others users who are neither the file's owner nor members of the file's group
a all All three of the above, same as ugo

[operator]
Operator Description
+ Adds the specified modes to the specified classes
- Removes the specified modes from the specified classes
= The modes specified are to be made the exact modes for the specified classes


[mode]
Mode Permission
r Permission to read the file.
w Permission to write (or delete) the file.
x Permission to execute the file, or, in the case of a directory, search it.

[mode]
4 Permission to read the file.
2 Permission to write (or delete) the file.
1 Permission to execute the file, or, in the case of a directory, search it.
0 No Permission

So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1(read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute).

Example:

chmod u=rwx,g=rx,o=r myfile
chmod 754 myfile

chown


The chown prompt changes file or group ownership. It gives admins the option to change ownership of all the objects within a directory tree, as well as the ability to view information on the objects processed.

Syntax: 
chown [-c|--changes] [-v|--verbose] [-f|--silent|--quiet] [--dereference] [-h|--no-dereference] [--preserve-root] [--from=currentowner:currentgroup] [--no-preserve-root] [-R|--recursive] [--preserve-root] [-H] [-L] [-P] {new-owner|--reference=ref-file} file ...


new-owner formDescription
userThe name of the user to own the file. In this form, the colon (“:“) and the group is omitted. The owning group is not altered.
user:groupThe user and group to own the file, separated by a colon, with no spaces in between.
:groupThe group to own the file. In this form, user is omitted, and the group must be preceded by a colon.
user:If group is omitted, but a colon follows user, the owner is changed to user, and the owning group is changed to the login group of user.
:Specifying a colon with no user or group is accepted, but ownership will not be changed. This form does not cause an error, but changes nothing.
Options
OptionDescription
-c,
–changes
Similar to –verbose mode, but only displays information about files that are actually changed. For example:

changed ownership of ‘dir/dir1/file1’ from hope:neil to hope:hope
-v,
–verbose
Display verbose information for every file processed. For example:

changed ownership of ‘dir/dir1/file1’ from hope:neil to hope:hope ownership of ‘dir/dir1’ retained as hope:hope
-f,
–silent,
–quiet
Quiet mode. Do not display output.
–dereferenceDereference all symbolic links. If file is a symlink, change the owner of the referenced file, not the symlink itself. This is the default behavior.
-h,
–no-dereference
Never dereference symbolic links. If file is a symlink, change the owner of the symlink rather than the referenced file.
–from=currentowner:currentgroupChange the owner or group of each file only if its current owner or group match currentowner and/or currentgroup. Either may be omitted, in which case a match is not required for the other attribute.
–no-preserve-rootDo not treat / (the root directory) in any special way. This is the default behavior. If the –preserve-root option is previously specified in the command, this option will cancel it.
–reference=ref-fileUse the owner and group of file ref-file, rather than specifying ownership with new-owner.
-R,
–recursive
Operate on files and directories recursively. Enter each matching directory, and operate on all its contents.

Recursive options

The following options modify how a hierarchy is traversed when the -R or –recursiveoption is specified.

OptionDescription
–preserve-rootNever operate recursively on the root directory /.

If –recursive is not specified, this option has no effect.
-HIf a file specified on the command line is a symbolic link to a directory, traverse it and operate on those files and directories as well.
-LTraverse all symbolic links to a directories.
-PDo not traverse any symbolic links; operate on the symlinks themselves. This is the default behavior.

If more than one of -H-L, or -P is specified, only the final option takes effect.

Other options

These options display information about the program, and cannot be used with other options or arguments.

OptionDescription
–helpDisplay a brief help message and exit.
–versionDisplay version information and exit.

Exit status

chown exits with a status of 0 for success. Any other number indicates failed operation.

Example:   
sudo chown myuser myfile.txt
sudo chown notme:notmygroup myfile.txt
sudo chown -R myuser:mygroup otherfiles

cmp


The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same. If they differ, cmp reports the byte and line number where the first difference occurred.

Syntax:  
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]


[OPTION]
-b --print-bytes
Print differing bytes.
-i SKIP --ignore-initial=SKIP
Skip the first SKIP bytes of input.
-i SKIP1:SKIP2 --ignore-initial=SKIP1:SKIP2
Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.
-l --verbose
Output byte numbers and values of all differing bytes.
-n LIMIT --bytes=LIMIT
Compare at most LIMIT bytes.
-s --quiet --silent
Output nothing; yield exit status only.
-v --version
Output version info.
--help
Output this help.
SKIP1 and SKIP2 are the number of bytes to skip in each file. SKIP values may be followed by the following multiplicative suffixes: kB 1000, K 1024, MB 1,000,000, M 1,048,576, GB 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y.
If a FILE is '-' or missing, read standard input.


Example:

cmp -b file1.txt file2.txt
cmp -i 10 file1.txt file2.txt

comm


Admins use comm to compare lines common to file1 and file2. The output is in three columns; from left to right: lines unique to file1, lines unique to file2 and lines common in both files.

Syntax:  
comm [OPTION]... FILE1 FILE2


Example:
comm file1.txt file2.txt

cp


The cp command copies files and directories. Copies can be made simultaneously to another directory even if the copy is under a different name.

Syntax:  
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory


Example:
cp Src_file1 Src_file2 Src_file3 Dest_directory

cpio


The cpio command copies files into or out of a cpio or tar archive. A tar archive is a file that contains other files, plus information about them, such as their file name, owner, timestamps and access permissions. The archive can be another file on the disk, a magnetic tape or a pipe. It also has three operating modes: copy-out, copy-in and copy-pass. It is also­ a more efficient alternative to tar.

GNU cpio is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files.

The following archive formats are supported: binary, old ASCII, new ASCII, CRCHPUXbinary, HPUX old ASCII, old tar, and POSIX.1 tar. The tar format is provided for compatibility with the tar program. By default, cpio creates binary format archives, for compatibility with older cpio programs. When extracting from archives, cpio automatically recognizes which kind of archive it is reading and can read archives created on machines with a different byte-order.

Copy-Out Mode Syntax:

In copy-out mode, cpio copies files into an archive. It reads a list of filenames, one per line, on the standard input and writes the archive onto the standard output. A typical way to generate the list of filenames is with the find command; you should give find the -depth option to minimize problems with permissions on directories that are unreadable. Copy-Out mode syntax:

cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] 
     [-O [[user@]host:]archive] [-F [[user@]host:]archive] 
     [--file=[[user@]host:]archive] [--format=format] 
     [--message=message][--null] [--reset-access-time] [--verbose] [--dot] 
     [--append] [--block-size=blocks] [--dereference] [--io-size=bytes] 
     [--rsh-command=command] [--help] [--version] < name-list [> archive]

Copy-In Mode Syntax:

In copy-in mode, cpio copies files out of an archive or lists the archive contents. It reads the archive from the standard input. Any non-option command line argumentsare shell globbing patterns; only files in the archive whose names match one or more of those patterns are copied from the archive. Unlike in the shell, an initial ‘.‘ in a filename does match a wildcard at the start of a pattern, and a ‘/‘ in a filename can match wildcards. If no patterns are given, all files are extracted. Copy-In mode syntax:

cpio {-i|--extract} [-bcdfmnrtsuvBSV] [-C bytes] [-E file] [-H format] 
     [-M message] [-R [user][:.][group]] 
     [-I [[user@]host:]archive] [-F [[user@]host:]archive] 
     [--file=[[user@]host:]archive] [--make-directories] [--nonmatching] 
     [--preserve-modification-time] [--numeric-uid-gid] [--rename] [-t|--list] 
     [--swap-bytes] [--swap] [--dot] [--unconditional] [--verbose] 
     [--block-size=blocks] [--swap-halfwords] [--io-size=bytes] 
     [--pattern-file=file] [--format=format] [--owner=[user][:.][group]] 
     [--no-preserve-owner] [--message=message] [--force-local] 
     [--no-absolute-filenames] [--absolute-filenames] [--sparse] 
     [--only-verify-crc] [--to-stdout] [--quiet] [--rsh-command=command] 
     [--help] [--version] [pattern...] [< archive]

Copy-Pass Mode Syntax:

In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument. Copy-Pass mode syntax:

cpio {-p|--pass-through} [-0adlmuvLV] [-R [user][:.][group]] [--null] 
     [--reset-access-time] [--make-directories] [--link] [--quiet] 
     [--preserve-modification-time] [--unconditional] [--verbose] [--dot] 
     [--dereference] [--owner=[user][:.][group]] [--no-preserve-owner] 
     [--sparse] [--help] [--version] destination-directory < name-list

CPIO Options

-0–nullRead a list of filenames terminated by a null character, instead of a newline, so that files whose names contain newlines can be archived. GNU find is one way to produce a list of null-terminated filenames. This option may be used in copy-out and copy-pass modes.
-a–reset-access-timeReset the access times of files after reading them, so that it does not look like they have just been read.
-A–appendAppend to an existing archive. Only works in copy-out mode. The archive must be a disk file specified with the -O or -F (-file) option.
-b–swapSwap both halfwords of words and bytes of halfwords in the data. Equivalent to -sS. This option may be used in copy-in mode. Use this option to convert 32-bit integers between big-endian and little-endian machines.
-BSet the I/O block size to 5120 bytes. Initially the block size is 512 bytes.
–block-size=BLOCK-SIZESet the I/O block size to BLOCK-SIZE * 512 bytes.
-cIdentical to ‘-H newc‘; uses the new (SVR4) portable format. If you want the old portable (ASCII) archive format, use ‘-H odc‘ instead.
-C IO-SIZE–io-size=IO-SIZESet the I/O block size to IO-SIZE bytes.
-d–make-directoriesCreate leading directories where needed.
-E FILE–pattern-file=FILERead additional patterns specifying filenames to extract or list from FILE. The lines of FILE are treated as if they had been non-option arguments to cpio. This option is used in copy-in mode.
-f–nonmatchingOnly copy files that do not match any of the given patterns.
-F–file=archiveArchive filename to use instead of standard input or output. To use a tape drive on another machine as the archive, use a filename that starts with ‘HOSTNAME:‘. The hostname can be preceded by a username and an ‘@‘ to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user’s ‘~/.rhosts‘ file).
–force-localWith -F-I, or -O, take the archive file name to be a local file even if it contains a colon, which would ordinarily indicate a remote host name.
-H FORMAT–format=FORMATUse archive format FORMAT. The valid formats are listed below; the same names are also recognized in all-caps. The default in copy-in mode is to automatically detect the archive format, and in copy-out mode is ‘bin‘.

bin: The obsolete binary format.

odc: The old (POSIX .1) portable format.

newc: The new (SVR4) portable format, which supports file systems having more than 65536 inodes.

crc: The new (SVR4) portable format with a checksum added.

tar: The old tar format.

ustar: The POSIX .1 tar format. Also, recognizes GNU tar archives, which are similar but not identical.

hpbin: The obsolete binary format used by HPUX’s cpio (which stores device files differently).

hpodc: The portable format used by HPUX’s cpio (which stores device files differently).
-i–extractRun in copy-in mode. (see ‘Copy-in mode‘).
-I archiveArchive filename to use instead of standard input. To use a tape drive on another machine as the archive, use a filename that starts with ‘HOSTNAME:‘. The hostname can be preceded by a username and an ‘@‘ to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user’s ‘~/.rhosts‘ file).
-kIgnored; for compatibility with other versions of cpio.
-l–linkLink files instead of copying them, when possible.
-L–dereferenceCopy the file that a symbolic link points to, rather than the symbolic link itself.
-m–preserve-modification-timeRetain previous file modification times when creating files.
-M MESSAGE–message=MESSAGEPrint MESSAGE when the end of a volume of the backup media (such as a tape or a floppy disk) is reached, to prompt the user to insert a new volume. If MESSAGE contains the string ‘%d‘, it is replaced by the current volume number (starting at 1).
-n–numeric-uid-gidShow numeric UID and GID instead of translating them into names when using the ‘–verbose‘ option.
–no-absolute-filenamesCreate all files relative to the current directory in copy-in mode, even if they have an absolute file name in the archive.
–absolute-filenamesThis is the default: tell cpio not to strip leading file name components that contain ‘..‘ and leading slashes from file names in copy-in mode.
–no-preserve-ownerDo not change the ownership of the files; leave them owned by the user extracting them. This is the default for non-root users, so that users on System V don’t inadvertently give away files. This option can be used in copy-in mode and copy-pass mode.
-o–createRun in copy-out mode. (see ‘Copy-out mode‘).
-O archiveArchive filename to use instead of standard output. To use a tape drive on another machine as the archive, use a filename that starts with ‘HOSTNAME:‘. The hostname can be preceded by a username and an ‘@‘ to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user’s ‘~/.rhosts‘ file).
–only-verify-crcVerify the CRC of each file in the archive, when reading a CRC format archive. Do not actually extract the files.
-p–pass-throughRun in copy-pass mode. (see ‘Copy-pass mode‘).
–quietDo not print the number of blocks copied.
-r–renameInteractively rename files.
-R [user][:.][group], –owner [user][:.][group]Set the ownership of all files created to the specified user and/or group in copy-out and copy-pass modes. Either the user, the group, or both, must be present. If the group is omitted but the ‘:‘ or ‘.‘ separator is given, use the given user’s login group. Only the super-user can change files’ ownership.
–rsh-command=COMMANDNotifies cpio that is should use COMMAND to communicate with remote devices.
-s–swap-bytesSwap the bytes of each halfword (pair of bytes) in the files. This option can be used in copy-in mode.
-S–swap-halfwordsSwap the halfwords of each word (4 bytes) in the files. This option may be used in copy-in mode.
–sparseWrite files with large blocks of zeros as sparse files. This option is used in copy-in and copy-pass modes.
-t–listPrint a table of contents of the input.
–to-stdoutExtract files to standard output. This option may be used in copy-in mode.
-u–unconditionalReplace all files, without asking whether to replace existing newer files with older files.
-v–verboseList the files processed, or with ‘-t‘, give an ‘ls -l‘ style table of contents listing. In a verbose table of contents of a ustararchive, user and group names in the archive that do not exist on the local system are replaced by the names that correspond locally to the numeric UID and GID stored in the archive.
-V–dotPrint a ‘.‘ for each file processed.
–versionPrint the cpio program version number and exit.

cpio examples

When creating an archive, cpio takes the list of files to be processed from the standard input, and then sends the archive to the standard output, or to the device defined by the ‘-F‘ option. Usually find or ls is used to provide this list to the standard input. In the following example you can see the possibilities for archiving the contents of a single directory:

% ls | cpio -ov > directory.cpio

The ‘-o‘ option creates the archive, and the ‘-v‘ option prints the names of the files archived as they are added. Notice that the options can be put together after a single ‘‘ or can be placed separately on the command line. The ‘>‘ redirects the cpio output to the file ‘directory.cpio‘.

If you wanted to archive an entire directory tree, the find command can provide the file list to cpio:

% find . -print -depth | cpio -ov > tree.cpio

This will take all the files in the current directory, the directories below and place them in the archive tree.cpio. Again the ‘-o‘ creates an archive, and the ‘-v‘ option shows you the name of the files as they are archived (see ‘Copy-out mode‘). Using the ‘.‘ in the find statement will give you more flexibility when doing restores, as it will save file names with a relative path via a hard-wired, absolute path. The ‘-depth‘ option forces ‘find‘ to print of the entries in a directory before printing the directory itself. This limits the effects of restrictive directory permissions by printing the directory entries in a directory before the directory name itself.

Extracting an archive requires a bit more thought because cpio will not create directories by default. Another characteristic, is it will not overwrite existing files unless specified.

% cpio -iv < directory.cpio

This will retrieve the files archived in the file directory.cpio and place them in the present directory. The ‘-i‘ option extracts the archive and the ‘-v‘ shows the file names as they are extracted. If you are dealing with an archived directory tree, you need to use the ‘-d‘ option to create directories as necessary, something like:

% cpio -idv < tree.cpio

This will take the contents of the archive tree.cpio and extract it to the current directory. If you try to extract the files on top of files of the same name that already exist (and have the same or later modification time) cpio will not extract the file unless told to do so by the -u option (see ‘Copy-in mode‘).

In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument (see ‘Copy-pass mode‘).

% find . -depth -print0 | cpio --null -pvd new-dir

The example shows copying the files of the present directory, and sub-directories to a new directory called new-dir. Some new options are the ‘-print0‘ available with GNU find, combined with the ‘–null‘ option of cpio. These two options act together to send file names between find and cpio, even if special characters are embedded in the file names. Another is ‘-p‘, which tells cpio to pass the files it finds to the directory ‘new-dir‘.

find . -print | cpio -ocv > /dev/fd0

Above, using the find command would list all files and directories and using the cpiocommand copy those files listed to the floppy drive.

find . -print | cpio -dumpv /home/users/hope

In the above example, the find command would list all files and subdirectories of the current directory, and pipe them to the cpio command, which copies those files to the hope user account.

cpio -icuvd < /dev/fd0

The above command would restore the files back from the floppy.

Syntax: 



Example:

CRON


CRON is a Linux system process that executes a program at a preset time. To use a CRON script, admins must prepare a text file that describes the program and when they want CRON to execute it. Then, the crontab program loads the text file and executes the program at the specified time.

System Cron jobs exist as entries in the /etc/crontab file. Each job is described on a single line by defining a time interval, a user to run the command as, and the command to run. Cron can run any kind of script, command, or executable.

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /        cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /        cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Below is the default system crontab file from Debian 9:

The first job in the Cron table is:

`17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly`.

This means at 17 minutes past each hour, change directory to /, the root of the filesystem. Then, as the root user, run the run-parts binary to execute all jobs in /etc/cron.hourly.

Time intervals are denoted by numbers and operators filled in place of each asterisk in a Cron job’s crontab line. From left to right, the asterisks represent:

  • Minutes specified as a number from 0 to 59.
  • Hours specified as numbers from 0 to 23.
  • Days of the month, specified as numbers from 1 to 31.
  • Months specified as numbers from 1 to 12.
  • Days of the week, specified as numbers from 0 to 7, with Sunday represented as either/both 0 and 7.

cURL


Admins use cURL to transfer a URL. It is useful for determining if an application can reach another service and how healthy the service is.

Syntax: 



Example:

D

declare


The declare command states variables, gives them attributes or modifies the properties of variables.

df


This command displays the amount of disk space available on the file system containing each file name argument. With no file name, the df command shows the available space on all the currently mounted file systems.

his command displays the amount of disk space available on the file system containing each file name argument. With no file name, the df command shows the available space on all the currently mounted file systems.

E

echo


Use echo to repeat a string variable to standard output.

enable


The enable command stops or starts printers and classes.

env


The env command runs a program in a modified environment or displays the current environment and its variables.

eval


The eval command analyzes several arguments, concatenates them into a single command and reports on that argument’s status.

exec


This function replaces the parent process with any subsequently typed command. The exec command treats its arguments as the specification of one or more subprocesses to execute.

exit


The exit command terminates a script and returns a value to the parent script.

expect


The expect command talks to other interactive programs via a script and waits for a response, often from any string that matches a given pattern.

export


The export command converts a file into a different format than its current format. Once a file is exported, it can be accessed by any application that uses the new format.

F

find
The find command searches the directory tree to locate particular groups of files that meet specified conditions, including -name, -type, -exec, -size, -mtime and -user.

forwhile


The for and while commands execute or loop items repeatedly as long as certain conditions are met.

free


With the free command, admins can see the total amount of free and used physical memory and swap space in the system, as well as the buffers and cache used by the kernel.

G

gawk
See AWK.

grep


The grep command searches files for a given character string or pattern and can replace the string with another. This is one method of searching for files within Linux.

gzip


This is the GNU Project’s open source program for file compression that compresses webpages on the server end for decompression in the browser. This is popular for streaming media compression and can simultaneously concatenate and compress several streams.

H

history


The history function shows all the commands used since the start of the current session.

I

ifconfig


The iconfig command configures kernel-resident network interfaces at boot time. It is usually only needed when debugging or during system tuning.

ifup


With ifup, admins can configure a network interface and enable a network connection.

ifdown


The ifdown command shuts down a network interface and disables a network connection.

iptablesThe iptables command allows or blocks traffic on a Linux host and can prevent certain applications from receiving or transmitting a request.

K

kill


With kill signals, admins can send a specific signal to a process. It is most often used to safely shut down processes or applications.

L

less 


The less command lets an admin scroll through configuration and error log files, displaying text files one screen at a time with backward or forward navigation available.

locate


The locate command reads one or more databases and writes file names to match certain output patterns.

lft


The lft command determines connection routes and provides information to debug connections or find a box/system location. It also displays route packets and file types.

ln


The ln command creates a new name for a file using hard linking, which allows multiple users to share one file.

ls


The ls command lists files and directories within the current working directory, which allows admins to see when configuration files were last edited.

lsof


Admins use lsof to list all the open files. They can add -u to find the number of open files by username.

lsmod


The lsmod command displays a module’s status within the kernel, which helps troubleshoot server function issues.

man


The man command allows admins to format and display the user manual that’s built into Linux distributions, which documents commands and other system aspects.


The man command allows admins to format and display the user manual that’s built into Linux distributions, which documents commands and other system aspects.

more


Similar to less, more pages through text one screen at a time, but has limitations on file navigation.

mount


This command mounts file systems on servers. It also lists the current file systems and their mount locations, which is useful to locate a defunct drive or install a new one.

mkdir


Linux mkdir generates a new directory with a name path.

N

neat


Gnome GUI tool that allows admins to specify the information needed to set up a network card.

netconfig/netcfg


Admins can use netconfig to configure a network, enable network products and display a series of screens that ask for configuration information.

netstat


This command provides information and statistics about protocols in use and current TCP/IP network connections. It is a helpful forensic tool for figuring out which processes and programs are active on a computer and are involved in network communications.

nslookup


A user can enter a host name and find the corresponding IP address with nslookup. It can also help find the host name.

od


The od command dumps binary files in octal — or hex/binary — format to standard output.

passwd
Admins use passwd to update a user’s current password.

ping  

     
The ping command verifies that a particular IP address exists and can accept requests. It can test connectivity and determine response time, as well as ensure an operating user’s host computer is working.

ps


Admins use ps to report the statuses of current processes in a system.

pwd


The print working directory (pwd) command displays the name of the current working directory.

read


The read command interprets lines of text from standard input and assigns values of each field in the input line to shell variables for further processing.

rsync


This command syncs data from one disk or file to another across a network connection. It is similar to rcp, but has more options.

screen


The GNU screen utility is a terminal multiplexor where a user can use a single terminal window to run multiple terminal applications or windows.

sdiff


Admins use sdiff to compare two files and produce a side-by-side listing indicating lines that are dissimilar. The command then merges the files and outputs the results to the outfile.

sed


The sed utility is a stream editor that filters text in a pipeline, distinguishing it from other editors. It takes text input, performs operations on it and outputs the modified text. This command is typically used to extract part of a file using pattern matching or to substitute multiple occurrences of a string within a file.

service


This command is the quickest way to start or stop a service, such as networking.

shutdown


The shutdown command turns off the computer and can be combined with variables such as -h for halt after shutdown or -r for reboot after shutdown.

slocate


Like locate, slocate, or secure locate, provides a way to index and quickly search for files, but it can also securely store file permissions and ownership to hide information from unauthorized users.

Snort


Snort is an open source network intrusion detection system and packet sniffer that monitors network traffic. It looks at each packet to detect dangerous payloads or suspicious anomalies. Snort is based on libpcap.

sort


This command sorts lines of text alphabetically or numerically according to the fields. Users can input multiple sort keys.

sudo


The sudo command lets a system admin give certain users the ability to run some — or all — commands at the root level and logs all the commands and arguments.

SSH


SSH is a command interface for secure remote computer access and is used by network admins to remotely control servers.

tar


The tar command lets users create archives from a number of specified files or to extract files from a specific archive.

tail        

The tail command displays the last few lines of the file. This is particularly helpful for troubleshooting code because admins don’t often need all the possible logs to determine code errors.

TOP


TOP is a set of protocols for networks that performs distributed information processing and displays the tasks on the system that take up the most memory. TOP can sort tasks by CPU usage, memory usage and runtime.

touch


Admins can create a blank file within Linux with the touch command.

tr


This command translates or deletes characters from a text stream. It writes to a standard output, but it does not accept file names as arguments — it only accepts input from standard input.

traceroute


The traceroute function determines and records a route through the internet between two computers and is useful for troubleshooting network/router issues. If the domain does not work or is not available, admins can use traceroute to track the IP.

uname


This function displays the current operating system name and can print system information.

uniq


With uniq, admins can compare adjacent lines in a file and remove or identify any duplicate lines. 

vi


The vi environment is a text editor that allows a user to control the system with just the keyboard instead of both mouse selections and keystrokes.

vmstat
The vmstat command snapshots everything in a system and reports information on such items as processes, memory, paging and CPU activity. This is a good method for admins to use to determine where issues/slowdown may occur in a system.

wget


This is a network utility that retrieves web files that support HTTP, HTTPS and FTP protocols. The wget command works non-interactively in the background when a user is logged off. It can create local versions of remote websites and recreate original site directories.

while 


See for.

whoami


The whoami command prints or writes the user login associated with the current user ID to the standard output.

xargs


Admins use xargs to read, build and execute arguments from standard input. Each input is separated by blanks.

How to Press or Fire Keyboard Event by Shell Scripting Technique

In most of the programming language the key press events are not comes as inbuilt feathers.At that point of time we need support of the shell scripting technique.In this post you will find the use and the different shortcut key available for this .

For VB Shell Scripting Scripting we need to create the code using set command as below

‘Set WshShell = WScript.CreateObject(“WScript.Shell”)
‘WshShell.Run “%windir%\notepad”
set objSendKey=CreateObject(“WScript.shell”)
set objSetFocus = Window(“nativeclass:=Notepad”,”index:=0″)
SystemUtil.Run “notepad.exe”, “”, “”, ”
wait 2
objSetFocus.Move 10,10
wait 1
objSetFocus.Click
[smartads]

1. How do I send an enter keystroke using QTP?

You could use the ASCII character for a carriage return Chr(13)

objSendKey.SendKeys(Chr(13))

(For more ASCII codes to use in QTP check out :QTP ASCII CHR() CODE CHART )

or use the Tilde character:
objSendKey.SendKeys(“~”)

Both will emulate an ‘Enter’ key.

2. How do I send a space?

To send a space, send the string ” “

3. How do I to send multiple keystrokes at one time using QTP’s VBscript?

To send multiple keys you can create compound string arguments. For example the following will hold down the Ctrl key, press the H key for the ‘Replace’ and type joe into the Replace window’s find textbox:
objSendKey.SendKeys(“^(h)joe”

)

4. How do I send a SHIFT, CTRL or Alt keystroke?

The special character for the Shift key in vbscript is the + sign.
For example to hold down the shift key and type a string all in capital letters try this:
objSendKey.SendKeys(“+(joecolantonio)”)

The special character for the Ctrl key in vbscript is the ^ sign and the character for the Alt key in vbscript is the % sign
Keystroke Equivalent
Alt %
Ctrl ^
Shift +

5. How do a send a right mouse click?

Try sending a a shift F10:
objSendKey.

SendKeys(“+{F10}”);

6. Are there any Sendkeys best practices?

There are a few I can think of :

Always move the application to a known start position.
Always set focus to the object you want to interact with before using SendKeys.
For synchronization issues use the .exist or wait functionality often in your script.

For Example:
objSetFocus.Move 10,10
wait 1
objSetFocus.Click

7. How do I send parenthesis using SendKeys?

Parenthesis are special characters in QTP so one way to do this is to use a combination of the shift key with the numeric nine and zero keys:
objSendKey.SendKeys(“+9”)
objSendKey.SendKeys(“joe colantonio”)
objSendKey.SendKeys(“+0”)

8. My SendKeys is not working when I try to send a multiple values.What should I do?

If QTP’s SendKeys is not performing as expected try sending each keystroke a separate line.

9. What is the QTPs equivalent for shell available in the market?

In QTP you can use the Run Method which can be used to run a file or an application. For example to start notepad:
SystemUtil.Run “notepad.exe”, “”, “”, “”

10. How to do a select a checkbox or a row in an object?

If the object like a checkbox has focus sending a blank space should select it:
objSendKey.SendKeys(” “)

11. How do I repeat a keystroke multiple times?

This only works for singe keystrokes but if you wanted to type a letter five times you could use this shortcut:
objSendKey.SendKeys(“{J 5}”)

12. What is VBScript’s equivalent for keystroke :

These are the short code for the equivalent keystroke available for the particular key.
Keystroke Equivalent
Alt %
Backspace {BACKSPACE}
Break {BREAK}
Caps Lock {CAPSLOCK}
Ctrl ^
Delete {DELETE}
Down Arrow {DOWN}
End {END}
Esc {ESC}
Help {HELP}
Home {HOME}
Insert {INSERT}
Left Arrow {LEFT}
Num Lock {NUMLOCK}
Page Down {PGDN}
Page Up {PGUP}
Print Screen {PRTSC}
Right Arrow {RIGHT}
Scroll Lock {SCROLLOCK}
Shift +
Tab {TAB}
UP Arrow {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}

13. What are the QTP VBScript’s string constants for non-visible characters in strings?

Constant Value Description
VbCr Chr(13) Carriage return
VbCrLf Chr(13) & Chr(10) Carriage return and a Linefeed
VbLf Chr(10) Line Feed
VbNewLine Chr(13) & Chr(10) New Line
VbTab Chr(9) Horizontal tab

14. I don’t see the action I need to perform in the chart above – what should I do?

Try the Device Replay method instead – check out the Device Replay chart (QTP DEVICE REAPLY CODE CHART )

15. I’m using C# not QTP how do I start an application

Use Process() for example:
using System;
using System.Diagnostics;
using System.Windows.Forms;
Process myProcess = new Process():
myProcess.StartInfo.FileName = “cmd”;
myProcess.Start();

16. In CSharp what are the SendKeys methods?

Flush() – processes all Window messages in the queue

Send() – this sends keystrokes to an app

SendWait() – Sends keystrokes to an app and waits for the keystrokes to complete.

More Info:

If you found this helpful you might want to also check out my post 3 ways to use keyboard input in QuickTest Professional: Type, SendKeys and Device Replay.

Bibliomaniacs:

And as always for my fellow bibliomaniacs who may want to dive deeper into SendKeys , I would also recommend these two books:

1. VBScript Programmer’s Reference. (Sendkey info starts on page 338)

2. A Tester’s Guide to .NET Programming (Expert’s Voice) – This book is for the automation imagineer who may want to create a simple custom GUI sendkeys app (check out page 173 of this book)