Professional Documents
Culture Documents
LP UNIT II Qtionbanks
LP UNIT II Qtionbanks
LP UNIT II Qtionbanks
Write a grep command to display the lines which does not matches all the given
pattern.
or
6. Show line number while displaying the output using grep -n : To show the line number of file
with the line matched.
$ grep -n "unix" geekfile.txt
Output:
Agarwal
Aggarwal
Agrawal
$grep –f pattern.txt geekfile.txt
When we type something into our terminal program, we’ll often see output. For
example:
$ echo hello
hello
As we can see, echo hello is a command that means “output hello”. But where does
that output really go?
Standard output
Every Unix-based operating system has a concept of “a default place for output to
go”. Since that phrase is a mouthful, everyone calls it “standard output”, or “stdout”,
pronounced standard out. Your shell (probably bash or zsh) is constantly watching
that default output place. When your shell sees new output there, it prints it out on
the screen so that you, the human, can see it. Otherwise echo hello would send “hello”
to that default place and it would stay there forever.
Standard input
Standard input (“stdin”, pronounced standard in) is the default place where
commands listen for information. For example, if you type cat with no arguments, it
listens for input on stdin, outputting what you type to stdout, until you send it an
EOF character (CTRL+d):
$ cat
hello there
hello there
say it again
say it again
[ctrl+d]
As you can see, with standard input, you can send a string to a command directly.
As you can see, with standard input, you can send a string to a command
directly.
Pipes
Pipes connect the standard output of one command to the standard input
of another. You do this by separating the two commands with the pipe
symbol (|). Here’s an example:
$ echo "hello there"
hello there
$ echo "hello there" | sed "s/hello/hi/"
hi there
sed takes that output as its input and replaces “hello” with “hi”, then prints
out that result to stdout. Your shell only sees the final result after it’s been
processed by sed, and prints that result to the screen.
Hey, if sed sends its result to standard out, can we pipe sed to another sed?
Yep!
$ echo "hello there" | sed "s/hello/hi/" | sed "s/there/robots/"
hi robots
Above, we’ve connected echo to sed, then connected that to another sed.
Pipes are great for taking output of one command and transforming it using
other commands like jq. They’re a key part of the Unix philosophy of “small
sharp tools”: since commands can be chained together with pipes, each
command only needs to do one thing and then hand it off to another
command.
3. Write about here document?
Here documents
← /dev/null discards unwanted output Home
command <<HERE
text1
text2
testN
$varName
HERE
This type of redirection tells the shell to read input from the current source (HERE) until a line containg only word
(HERE) is seen. HERE word is not subjected to variable name, parameter expansion, arithmetic
expansion, pathname expansion, or command substitution. All of the lines read up to that point are then used as the
standard input for a command. Files are processed in this manner are commonly called here documents. If you do
not want variable name, parameter expansion, arithmetic expansion, pathname expansion, or command
substitution quote HERE in a single quote:
command <<'HERE'
text1
text2
testN
$varName
HERE
Example
Use here document feature to give constant text to a command. For example the following command will count the
words for input:
Sample outputs:
But, how do you count lots of lines at a time? Use here document as follows:
wc -w <<EOF
> This is a test.
> Apple juice.
> 100% fruit juice and no added sugar, colour or preservative.
> EOF
Sample outputs:
16
The <<, reads the shell input typed after the wc command at the PS2 prompts, >) up to a line which is identical to
word EOF.
#!/bin/bash
# run tar command and dump data to tape
tar -cvf /dev/st0 /www /home 2>/dev/null
END_OF_EMAIL
chmod +x tapebackup1.sh
./tapebackup1.sh
Sample outputs:
Subject: Test
From: root <root@www-03.nixcraft.net.in>
Date: 12:57 Am
To: vivek@nixcraft.co.in
for VARIABLE in
for LIST...; do for i in 1 2 3; do echo $i iteration ; done
COMMANDS...; done
[ EXPRESSION ]
Check if EXPRESSION is true or false. See man test for a full
explanation of valid expressions.
[ -e FILENAME ] True iff the give file exists
[ $hippos -eq 5 ] True iff the variable hippos contains the number 5
[ $hippos = "five" ] True iff the variable hippos contains the string "five"
condition True iff the file with a filename given in the
[ ! -x $filename ]
variable filename is not executable.
[ -e FILE1 -o -d FILE2
] True when either FILE1 exists or FILE2 is a directory
57. Quoting
58. Quoting text causes Bash to interpret text differently. For example, a filename containing
spaces must be enclosed in single or double quotes, or Bash will pass it as multiple separate
command line tokens.
59. There are three different forms of quoting, each for a different purpose.
60. Single quotes (')
61. Every character inside these quotes is interpreted completely literally. No variable
substitutions are performed.
62. Double quotes (")
63. Many special characters, such as spaces, are significant. However, some are interpreted.
Notably, variable substitution with $ is allowed.
64. Backquotes (`)
65. Unlike the other two, this form does not prevent interpretation of the quoted tokens.
Instead, text inside is interpreted as a command line, and its output substituted into the
original command line
5. Explain with example the process of creation and execution of a shell script?
A new process is created because an existing process makes an exact copy of itself. This
child process has the same environment as its parent, only the process ID number is
different. This procedure is called forking.
After the forking process, the address space of the child process is overwritten with the new
process data. This is done through an exec call to the system.
The fork-and-exec mechanism thus switches an old command with a new, while the
environment in which the new program is executed remains the same, including
configuration of input and output devices, environment variables and priority. This
mechanism is used to create all UNIX processes, so it also applies to the Linux operating
system. Even the first process, init, with process ID 1, is forked during the boot procedure
in the so-called bootstrapping procedure.
This scheme illustrates the fork-and-exec mechanism. The process ID changes after the fork
procedure:
Every now and then things go wrong, even in good families. In an exceptional case, a
process might finish while the parent does not wait for the completion of this process. Such
an unburied process is called a zombie process.