Debug School

Pavani
Pavani

Posted on • Updated on

BASH SCRIPT AND ITS COMMANDS

BASH SCRIPT:
The Linux Bash is also known as 'Bourne-again Shell. 'A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a reusability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one or more times as per our requirements.
Example:
#!/bin/bash/
# it is a function
my function () {
hello world
}

function call

my function

Applications of Bash scripts:

  1. Manipulating files
  2. Executing routine tasks like Backup operation
  3. Automation Advantages of Bash Scripts:
  • It is simple.
  • It helps to avoid doing repetitive tasks
  • Easy to use
  • Frequently performed tasks can be automated
  • A sequence of commands can be run as a single command.
    Disadvantages of Bash Scripts:

  • Any mistake while writing can be costly.

  • A new process launched for almost every shell command executed.

  • Slow execution speed

  • Compatibility problems between different platforms.
    Some commands of bash:
    1) Basename: This command is used to strip directory and suffix from the file names.
    syntax: $ basename filename.txt .txt
    2) cal: displays the calender
    syntax: $ cal -y year
    3) df: disk file system. It will show the disk space usage in a tabular form. The df command is useful for discovering the available free space on a system or file system.
    syntax: $ df
    -> df -h: It displays the disk space in human readable form
    -> df -T: It displays the file system type.
    4) diff: diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. This command tells us which lines in one file have is to be changed to make the two files identical.
    -> diff uses certain special symbols and instructions that are required to make two files identical.
    a- add
    c- change
    d- delete
    syntax: $ diff file1.txt file2.txt
    -> -c : To view the differences in the context mode use -c option.
    -> -u: To view the differences in the unified mode use -u option.
    5)dir: This command is used to list the contents of a directory.
    syntax: $dir
    -> dir -a: displays all the hidden files(starting with .) along with two files denoted by . and .. which signals for current and previous directory respectively.
    -> dir -A: It is similar to -a option except that it does not display files that signals the current directory and previous directory.
    -> dir -l --author: displays the author of all the files.
    6) dmesg: driver message or display message used to examine the kernel ring buffer and print the message buffer of kernel. The output of this command contains the messages produced by the device drivers.
    syntax: dmesg
    -> dmesg | grep word-
    -> dmesg -t- -t specifies with timestamps.
    7) du- du command, short for disk usage, is used to estimate file space usage. The du command can be used to track the files and directories which are consuming excessive amount of space on hard disk drive.
    -> du -h: display the disk size in human readable form.
    -> du -a: printing all files including directories.
    -> du -c: print total size
    -> du -s: get the summary of file system
    8) egrep: egrep is a pattern searching command which belongs to the family of grep functions.
    syntax: egrep pattern file name
    -> -c: Used to counts and prints the number of lines that matched the pattern and not the lines.
    -> -v: It prints the lines that does not match with the pattern.
    -> -o: prints the matched part of the line but not the entire line.
    9) eval- eval is a built-in Linux command which is used to execute arguments as a shell command. It combines arguments into a single string and uses it as an input to the shell and execute the commands.
    syntax: eval arg
    example: eval c="clear"
    10) expand: expand which allows you to convert tabs into spaces in a file and when no file is specified it reads from standard input.
    syntax: expand filename
    11) expr: It evaluates the given expression and displays the output
    syntax: expr expression
    12) fold: fold command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output. Default width is 80
    syntax: fold filename
    -> fold -w[n] filename: we can limit width of the width by number of the columns. we can change the default width of 80.
    -> fold -b[n] filename: this option of fold command is used to limit the width of the output by the number of bytes rather than the number of columns.
    -> -s: This option is used to break the lines on spaces so that words are not broken. If a segment of the line contains a blank character within the first width column positions, break the line after the last such blank character meeting the width constraints.
    syntax: fold -w[n] -s filename
    13) free: free command which displays the total amount of free space available along with the amount of memory used and swap memory in the system, and also the buffers used by the kernel.
    syntax: $free
    14) gawk: gawk command in Linux is used for pattern scanning and processing language.
    $gawk -F'{print $1} filename
    15) id: this command used to find the user identity, group identity.
    syntax: $ id
    16) join: join command is used to join the two files based on a key field present in both the files. The input file can be separated by white space or any delimiter.
    syntax: join file1.txt file2.txt
    17) look: The look command in Linux shows the lines beginning with a given string. This command also uses binary search if the file is sorted. If file is not specified, the file /usr/share/dict/words is used.
    syntax: $look string filename
    18) ls: this command lists contents in a directory.
    syntax: ls -l
    19) more: more command is used to view the text files in the command prompt, displaying one screen at a time in case the file is large. The more command also allows the user do scroll up and down through the page.
    syntax: more filename
    20) nl: This command is used to count the numbering the lines syntax: $ nl filename
    21) paste: It is used to join files horizontally by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output.
    syntax: paste file1 file2
    22) ps- process status ps command is used to list the currently running processes and their PIDs along with some other information depends on different options
    syntax: ps -a
    23) rm: remove files
    syntax: $ rm filename
    -> $ rm -f filename: remove files forcefully without prompting for confirmation.
    24) rmdir: remove empty directories from the file system
    syntax: $rmdir directory
    25) SCP (secure copy): The SCP command or secure copy allows secure transferring of files in between the local host and the remote host or between two remote hosts. It uses the same authentication and security as it is used in the Secure Shell (SSH) protocol. SCP is known for its simplicity, security and pre-installed availability.
    syntax: $ scp
    26) seq: is used to generate numbers from FIRST to LAST in steps of INCREMENT.
    syntax: seq first increment last
    seq 0 3 20
    27) set: It is used to set or unset specific flags and settings inside the shell environment. It can be used to change or display the shell attributes and parameters.
    -> -a- use to mark variables that are created or modified or created for export.
    -> -b- use to notify the termination of the job.
    -> -e- use to exit when the command exits with a non-zero status.
    -> -f- it disables the file name generation known as
    globbing
    -> -h- It saves the location of the command where it got looked.
    ->-k- It places all assignment arguments in the environment variable of a command.
    example: set -x
    echo apple orange mango
    27) sleep: This command helps delaying the execution
    syntax: sleep 10s
    s- second
    m- minutes
    h- hours
    28) sort: SORT command is used to sort a file, arranging the records in a particular order. SORT command sorts the contents of a text file, line by line.
    syntax: sort filename.txt
    -> sort -r filename.txt: sorts the file in reverse order.
    29) sudo: super user do. It will run the command with elevated privileges. This is equal to run as administrator command in windows.
    syntax: sudo -l
    30) sum: sum command in Linux is used to find checksum and count the blocks in a file. Basically, this command is used to show the checksum and block count for each specified file
    syntax: sum -r filename
    31) time: this command is used to print a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates. ‘real-time is the time elapsed wall clock time taken by a command to get executed, while ‘user ‘and ‘systems are the number of CPU seconds that command uses in user and kernel mode respectively.
    syntax: $time filename
    32) touch: This command will update the timestamps of input file if it exists and will create an empty file if the input file does not exist.
    syntax: touch -c filename
    33) top: this command shows the summary information of the system and the list of processes which are currently processed
    syntax: $ top
    34) tr: This command is used to translate or delete the characters. It translates upper case to lower case, deleting some characters.
    syntax: cat filename| tr [a-z] [A-Z]
    -> -s option is used to squeeze the repetitive occurrences.
    -> -d option is used to delete the characters.
    35) tty: It displays the information about the terminal. It prints the file name of the terminal connected to the standard input
    syntax: $tty
    36) type: This command is used to describe how arguments are converted when they are used as commands. It displays whether the command is builtin or binary file
    syntax: $ type command name
    37) ulimit: It is used to see, set, limit the usage of the resource usage of the current user.
    -> ulimit -a: to check the ulimit value
    -> ulimit -u: to display the maximum users process limit for logged in user.
    -> ulimit -F: to show maximum size of a user can have.
    38) uname: print system information.
    syntax: $uname
    39) wc- This word count (wc) is used to print word, line, byte counts.
    syntax: $ wc filename
    40) .- run a command script in the current shell.
    41) !!- run the last command again
    42) whoami- Print the current username and id
    syntax: $ whoami
    43) who- print all users currently logged in
    syntax: $ who
    44) vmstat: report virtual memory stastics.

Top comments (0)