Basic Script Building-Using Pipes

Using Pipes :

There are times when you need to send the output of one command to the input of another command.

Basic Script Building – Redirecting Input and Output

Redirecting Input and Output

At times, you may find it beneficial to store the results of a command rather than simply viewing them on the screen. The bash shell offers various operators for redirecting command output to different destinations, such as files. Additionally, redirection can manage input, enabling you to redirect a file to a command for processing. This section will outline the steps required to implement redirection in shell scripts.

Output Redirection:

The most basic type of redirection is sending output from a command to a file. The bash shell uses the greater-than symbol (>) for this:

command > outputfile

Sometime, instead of of overwriting the file content,you may need to append output from a command to existing file.For Example, if you’re creating a log fileto document an action on the system.In this stituation, you can use the double greater-than symbol (>>) to append data: 

[root@engrabhishekroshan scripts]# date;who >> outputredirection.txt
Sun Apr 7 01:08:18 AM IST 2024
[root@engrabhishekroshan scripts]# cat outputredirection.txt
root pts/0 2024-04-06 18:06 (192.168.72.1)
[root@engrabhishekroshan scripts]#

Input Redirection:

Input redirection is the opposite of output redirection. Instead of taking the output of a command and redirecting it to a file, input redirection takes the content of a file and redirects it to a command.

Basic Script Building – Using Multiple Commands

Using Multiple Commands

In Linux so far you have seen how to use the command-line Interface (CLI). For Example: if you run “date” command we get the out put date and time

[root@engrabhishekroshan /]# date
Wed Jan 31 06:15:38 PM IST 2024
[root@engrabhishekroshan /]#

For Example: If you run “who” command ,we get the output as which all users logged into the system.

[root@engrabhishekroshan /]# who
root pts/0 2024-01-26 10:16 (192.168.72.1)
[root@engrabhishekroshan /]#

Using multiple Commands we can combined these two commands into a single commands by using semicolon  ; 

[root@engrabhishekroshan /]# date;who
Wed Jan 31 06:22:05 PM IST 2024
root pts/0 2024-01-26 10:16 (192.168.72.1)
[root@engrabhishekroshan /]# 

Linux Shell Scripting Beginner To Expert Course //Course Content // Topic -1

Topic Sub-Topic
Shell Scripting Basics 
 Using Multiple commands
 Creating a script file
 Displaying Messages
 Using Variables
 Redirecting Input & Output
 Employing Pipes
 Performing Maths
 Exiting the Script
 Practical – Lab
Using Structured Commands 
 Working with the if-then statement 
 Exploring the if-then Statement 
 Nesting file 
 Trying the test Command
 Considering Compound Testing 
 Working with Advanced if-then feature
 Considering the case Command
 Practical Lab
More Structured Commands 
 Looking at for command 
 Trying the C-style for command
 Exploring the While Command
 Using the until command
 nesting Loop
 Looping on file Data
 Controlling the Loop
 Processing the Output of a Loop
 Practical Lab
Handling User Input 
 Passing Parameter
 Using Special Parameter Variable
 Being Shifty
 Working with Option
 Standardizing Option
 Getting user Input
 Practical Lab
Presenting Data 
 Understanding Input and Output
 Redirecting output in Scripts
 Redirecting Input in Scripts
 Creating Your own redirections
 Listing Open file Descriptors
 Supressing Command Output
 Using Temporary Files
 Logging Messages
 Practical lab
Script Control 
 Handling Signals
 Running Scripts in Background Mode
 Running Scripts without a hang-Up
 Controlling the Job
 Being Nice
 Running like Clockwork
 Practical Lab

Linux 100 + Realtime Scenario Based Interview Questions and Answers Explained in Detail | Part – 2

Question 11. What is NFS ? How to configure it ? What port no it does use ?

Answer:

  • To configure NFS we need to cionfigure NFS Server and NFS Client.
  • Steps to configure NFS Server.
  • a)Install nfs-util package.
  • b)Create a directory or directory on a partition and some data on it.
  • c)Now export the directory by editing /etc/exports file and using exportfs command.
  • d)Restart the service and make is permanent in /etc/fstab file.
  • Steps to configure NFS client.
  • a)check and install the NFS package if not installed.
  • b)start the NFS service
  • c)check which directory is exported for this machine using showmount command
  • d)Make a directory and mount the NFS dir over it.
  • e) Add some data to it and check the same is updated on server side.
  • Port No -2049

Linux 100 + Realtime Scenario Based Interview Questions and Answers Explained in Detail | Part – 1

Question 1. How to set a username and password to never expires ?

Answer: chage -M -1 krishna

Question 2. Why /etc/passwd and /etc/shadow file cannot be merged into one file ?

Answer:

  • The existence of the two files is a consequence of that /etc/passwd is a text file that can be read by other applications (as finger, ident or ls for example), so an attacker could gain access to the information of the file that included the hashed password.
  • To increase security, the hashed password that used to be in the file was moved to other file called /etc/shadow that is accessible only by root

Question 3 : How to list, all the files opened by particular PID ?

Answer:

  • lsof –p PID
  • Count number of files & processes

Question 4:  We are unable to unmount the file system. What are the reason behind it ?

Answer:

  • #you are in the same directory pwd ,
  • #some users are present in the directory and using its content fuser -cu /dev/sda,
  • #some of the files are open in the directory lsof /dev/sda7

Question 5 :  What could be the reason if server take more time after reboot ?

Answer:

  • filesystem got corrupt and its ext2,ext2 is not having journaling feature.

Question 6 : we are trying to create the file under any partition but we are getting        permission denied alert. What could be the reason? However,
    no space issue and no permission issue?

Answer : 

  • I am running out of inode
  • Sometimes, df command reports that there is enough free space but system claims file-system is full.
  • You need to check for the inode which identifies the file and its attributes on a file systems using the following command:
  • $ df -i
  • $ df -i /ftpusers/
 
  • Sample outputs:
  • Filesystem Inodes IUsed IFree IUse% Mounted on
  • /dev/sda8 6250496 11568 6238928 1% /ftpusers
  • So /ftpusers has 62,50,496 total inodes but only 11,568 are used. You are free to create another 62,38,928 files on /ftpusers partition.
  • If 100% of your inodes are used, try the following options:
  • Find unwanted files and delete or move to another server.

Find unwanted large files and delete or move to another server

error: Content is protected !!