Shell Scripting

Special Files in Linux

. Current directory

 .. Parent directory 

../ Parent directory, including slash; used to navigate from the parent 

../../ The parent of the parent directory 

~/ The current user’s home directory 

.hiddenfile Files that start with a dot are hidden files. They are generally configuration files


Wildcards

? Represents any one character 

* Represents any set of characters 

[xbc] Represents any one of the characters listed within the bracket 

[a-z] Represents any character between the defined range


History

View and edit commands previously run 

~/.bash_history: Location of your Bash history 

CTL + R: Searches the history of commands 

history: Shows a list of commands that have been previously run in a numbered list 

!: Shows specified command 

    !14 for example shows the command numbered 14 listed by history 

!xy: Runs the last command that starts with an xy 

!!: Runs the last command 

fc: Opens the previous command in a text editor; after closing the text editor, the modified command will be run in Bash


I/O 

I/O stands for Input / Output. 

    STDOUT: Standard output of command line programs

    STDIN: The source of input(s) for a program 

    STDERR: Standard error output of a command line program


Redirection 

These redirect the output or input of a command into files, devices, and the input of other commands.

> Redirects the standard output of a command into a file; replaces the contents of a file 

>> Appends into the end of a file 

< Imports the contents of a file into the command 

<< Appends the contents of a file into the command 

2> Redirects standard error of a command into a file 

2>> Appends standard error of a command into the end of a file 

&> Redirects standard error and standard output when redirecting text 

&>> Appends standard error and standard output when redirecting text 

    Example: cat < test.txt >> existingfile.txt 

    Uses the contents of test.txt on the cat command, then appends the results to existingfile.txt


Piping 

This processes commands on the output of other commands

Uses the standard output of a command prior to the pipe as the standard input for the command following the pipe 

<command1>  | <command2> Processes command2 based on the output command2 

Think of it as command layering


Lists (for "One Liners") 

In Bash, you can run multiple commands based on the following format: <Command> <option> <Command>

Options: 

; Run the following command even if the previous command fails or succeeds 

&& Run the following command only if the previous succeeds or has no errors 

|| Run the following command only if the previous fails or results in error 

& Run the previous command in the background


Command Substitution

Inserts command output into another context 

`Back Ticks` Input any bash command or set of commands 

$(Dollar Sign & Parenthesis) Input any bash command or set of commands 

    Examples: 

      `echo the current date is `date` Outputs the current date at the end of the string 

       file $(which login) Outputs the file type of the located command file

       echo "$(users | wc -w) users are logged in right now" Outputs users are logged in right now


Jobs 

Commands run from the terminal, whether in the foreground or in the background 

In the terminal, while running a command, you can use CTRL+Z to stop, but not kill, a command/job. You can start it up again later, either in the foreground or background. 

jobs Shows jobs and commands running in the background 

fg <job_number> Short for Foreground, and sends the specified job to the foreground of the terminal 

bg <job_number> Short for Background, and sends the specified job to the background of the terminal 

<Command> & Runs the command in the background, allowing you to run other commands while it processes 

nohup Runs a command immune to hang-ups and allows a command to run even after a terminal is closed or the user who ran the command is logged out


Text Processing

"Double Quotation marks" Meta-characters enclosed within the quotes are treated literally with the exception of variables which have already been set. 

Example: name=Cameron ; echo "My name is $name" 

'single quotation marks' All meta-characters processed literally, with no variable processing


Scripts

Basic Syntax 

#! /bin/bash 

# Commands 

Shebang / HashBang: #! /bin/bash 

    Informs Linux which command line interpreter to use for the script. In this example, it's the Bourne Again Shell


Environment Variables 

env Shows all environment variables 

env | grep EXAMPLE Prints current environment variables and then greps the result for the term EXAMPLE export EXAMPLE=VAR Exports shell variable EXAMPLE to the environment variables EXAMPLE=VAR ; export EXAMPLE Exports a previously-defined shell variable to the environment variables After you log off, the environment variables you set will restore to default. To permanently set an environment variable, you must either edit the user configuration files or global configuration files for Bash. 

      Add to .bashrc (for user): 

         ABC="123"; export ABC 

      Add to /etc/.bash.bashrc (for system): 

         ABC="123"; export ABC 


Linux Commands:-

useradd: Command that's used to create a local user account

usermod: Command that's used to modify a local user account

userdel: Command that's used to delete a local user account

groupadd: Command that's used to create a local group

groupmod: Command that's used to modify a local group

groupdel: Command that's used to delete a local group

passwd: Command that's most often used to assign passwords to user accounts, but it can be used for some other scenarios (for example, locking user accounts)

chage: Command that's used to manage user password expiry.


DirectoryUsage
/Root of the virtual directory, where normally, no files are placed
/binBinary directory, where many GNU user-level utilities are stored
/bootBoot directory, where boot files are stored
/devDevice directory, where Linux creates device nodes
/etcSystem configuration files directory
/homeHome directory, where Linux creates user directories, which are optional
/libLibrary directory, where system and application library files are stored
/libnameLibrary directory(ies), where alternative format system and application library files are stored, which is optional
/mediaMedia directory, a common place for mount points used for removable media
/mntMount directory, a common place used for temporarily mounting filesystems
/optOptional directory, where third-party software packages are stored
/procProcess directory, where current kernel, system, and process information is stored
/rootRoot user's home directory, which is optional
/runRun directory, where runtime data is held during system operation
/sbinSystem binary directory, where many GNU admin-level utilities are stored
/srvService directory, where local services store their files
/sysSystem directory, where devices, drivers, and some kernel feature information is stored
/tmpTemporary directory, where temporary work files can be created and destroyed
/usrUser directory, a secondary directory hierarchy
/varVariable directory, for files that change frequently, such as log files
  • A question mark (?) to represent one character
  • An asterisk (*) to represent any number of characters

DevOpsMap

Learn today for better tomorrow

0 Comments