Monday, January 2, 2017

Basic Shell Scripting Language

    https://bash.cyberciti.biz/guide/The_role_of_shells_in_the_Linux_environment 
    http://basiclinuxcommand.com/control-and-using-hardware-device-linux-system-using-linux-command.html
    Shell: A Command-Line Interpretor that connects a user to Operating System and allows to execute the commands or by creating text script.


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 .
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 .


1. basename /usr/bin/perlscript
This will remove the prefix, /usr/bin/, and prints only the string 'perlscript'

2. basename perlscript script
This will remove the suffix 'script' from 'perlscript' and prints only 'perl'
The Bourne shell program name is “shand it is typically located in the file system hierarchy at /bin/sh.
awk -F":" '{ print "UserName: " $1 "\tUserID:" $3 "\tFullName: " $5 }' /etc/passwd 

who -aH
 The who command with –aH option show the time of last system boot, show dead process, show system login process, show active process spawn by init, show current runlevel, show last system clock change, show list users logged in on the system and the –H to print line of column heading.   

how to check root directory in exciting sub directory
tree -L 1 /
    
 id++ id--
              variable post-increment and post-decrement
       ++id --id
              variable pre-increment and pre-decrement
       - +    unary minus and plus
       ! ~    logical and bitwise negation
       **     exponentiation
       * / %  multiplication, division, remainder
       + -    addition, subtraction
       << >>  left and right bitwise shifts
       <= >= < >
              comparison
       == !=  equality and inequality
       &      bitwise AND
       ^      bitwise exclusive OR
       |      bitwise OR
       &&     logical AND
       ||     logical OR
       expr?expr:expr
              conditional operator
       = *= /= %= += -= <<= >>= &= ^= |=
              assignment
       expr1 , expr2
 
 \a     alert (bell)
              \b     backspace
              \e     an escape character
              \f     form feed
              \n     new line
              \r     carriage return
              \t     horizontal tab
              \v     vertical tab
              \\     backslash
              \'     single quote
              \nnn   the eight-bit character whose value is the octal value nnn (one to three digits)
              \xHH   the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
              \cx    a control-x character

Use -e option of echo command to enable interpretation of backslash escapes.

Examples

echo "Pizza bill \$22.5"
echo -e "\a Ding dong\a"
The special parameters * and @ have special meaning when in double quotes, but you can disable them with the backslash:
echo "*"
echo "\*"
echo "\@"
finger :- 

Check Linux User Information using Linux finger Command.

 

  tilde (~) may be used to refer your own home directory or other users home directory

ls ~ display home directory

 If the tilde-prefix is a ~+, the value of the shell variable PWD replaces the tilde-prefix

  • If the tilde-prefix is a ~-, the value of the shell variable OLDPWD, if it is set, is substituted.

 

SequenceDescription
\aAn ASCII bell character (07)
\dThe date in "Weekday Month Date" format (e.g., "Tue May 26")
\eAn ASCII escape character (033)
\hThe hostname up to the first .
\HThe hostname (FQDN)
\jThe number of jobs currently managed by the shell
\lThe basename of the shell’s terminal device name
\nNewline
\rCarriage return
\sThe name of the shell, the basename of $0 (the portion following the final slash)
\tThe current time in 24-hour HH:MM:SS format
\TThe current time in 12-hour HH:MM:SS format
\@The current time in 12-hour am/pm format
\AThe current time in 24-hour HH:MM format
\uThe username of the current user
\vThe version of bash (e.g., 2.00)
\V TThe release of bash, version + patch level (e.g., 2.00.0)
\wThe current working directory, with $HOME abbreviated with a tilde
\WThe basename of the current working directory, with $HOME abbreviated with a tilde
\!The history number of this command
\#The command number of this command
\$If the effective UID is 0, a #, otherwise a $
\nnnThe character corresponding to the octal number nnn
\\A backslash
\[Begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\]End a sequence of non-printing characters</pre>

List currently configured shell options

set -o
To set shell variable option use the following syntax:  set -o variableName
To unset shell variable option use the following syntax:  set +o variableName

  $_- The default parameter for a lot of functions.
$.- Holds the current record or line number of the file handle that was last read. It is read-only and will be reset to 0 when the file handle is closed.
$/- Holds the input record separator. The record separator is usually the newline character. However, if $/ is set to an empty string, two or more newlines in the input file will be treated as one.
$,- The output separator for the print() function. Nor-mally, this variable is an empty string. However, setting $, to a newline might be useful if you need to print each element in the parameter list on a separate line.
$\-- Added as an invisible last element to the parameters passed to the print() function. Normally, an empty string, but if you want to add a newline or some other suffix to everything that is printed, you can assign the suffix to $.
$#-- The default format for printed numbers. Normally, it's set to %.20g, but you can use the format specifiers covered in the section "Example: Printing Revisited" in Chapter 9to specify your own default format.
$%-- Holds the current page number for the default file handle. If you use select() to change the default file handle, $% will change to reflect the page number of the newly selected file handle.
$=-- Holds the current page length for the default file handle. Changing the default file handle will change $= to reflect the page length of the new file handle.
$- -- Holds the number of lines left to print for the default file handle. Changing the default file handle will change $- to reflect the number of lines left to print for the new file handle.
$~-- Holds the name of the default line format for the default file handle. Normally, it is equal to the file handle's name.
$^-- Holds the name of the default heading format for the default file handle. Normally, it is equal to the file handle's name with _TOP appended to it.
$|-- If nonzero, will flush the output buffer after every write() or print() function. Normally, it is set to 0.
$$-- This UNIX-based variable holds the process number of the process running the Perl interpreter.
$?-- Holds the status of the last pipe close, back-quote string, or system() function.
$&-- Holds the string that was matched by the last successful pattern match.
$`- Holds the string that preceded whatever was matched by the last successful pattern match.
$'-- Holds the string that followed whatever was matched by the last successful pattern match.
$+-- Holds the string matched by the last bracket in the last successful pattern match. For example, the statement /Fieldname: (.*)|Fldname: (.*)/ && ($fName = $+); will find the name of a field even if you don't know which of the two possible spellings will be used.
$*-- Changes the interpretation of the ^ and $ pattern anchors. Setting $* to 1 is the same as using the /m option with the regular expression matching and substitution operators. Normally, $* is equal to 0.
$0-- Holds the name of the file containing the Perl script being executed.
$<number>-- This group of variables ($1, $2, $3, and so on) holds the regular expression pattern memory. Each set of parentheses in a pattern stores the string that match the components surrounded by the parentheses into one of the $<number> variables.
$[-- Holds the base array index. Normally, it's set to 0. Most Perl authors recommend against changing it without a very good reason.
$]-- Holds a string that identifies which version of Perl you are using. When used in a numeric context, it will be equal to the version number plus the patch level divided by 1000.
$"-- This is the separator used between list elements when an array variable is interpolated into a double-quoted string. Normally, its value is a space character.
$;-- Holds the subscript separator for multidimensional array emulation. Its use is beyond the scope of this book.
$!-- When used in a numeric context, holds the current value of errno. If used in a string context, will hold the error string associated with errno.
$@-- Holds the syntax error message, if any, from the last eval() function call.
$<- This UNIX-based variable holds the read uid of the current process.
$>-- This UNIX-based variable holds the effective uid of the current process.
$)-- This UNIX-based variable holds the read gid of the current process. If the process belongs to multiple groups, then $) will hold a string consisting of the group names separated by spaces.
$:-- Holds a string that consists of the characters that can be used to end a word when word-wrapping is performed by the ^ report formatting character. Normally, the string consists of the space, newline, and dash characters.
$^D-- Holds the current value of the debugging flags. For more information.
$^F-- Holds the value of the maximum system file description. Normally, it's set to 2. The use of this variable is beyond the scope of this book.
$^I-- Holds the file extension used to create a backup file for the in-place editing specified by the -i command line option. For example, it could be equal to ".bak."
$^L-- Holds the string used to eject a page for report printing.
$^P- This variable is an internal flag that the debugger clears so it will not debug itself.
$^T-- Holds the time, in seconds, at which the script begins running.
$^W-- Holds the current value of the -w command line option.
$^X-- Holds the full pathname of the Perl interpreter being used to run the current script.

How do I enable (set) and disable (unset) each option

To enable (set) each option, enter: 

shopt -s optionName
To disable (unset) each option, enter ;- shopt -u optionName 

 

 

 

Remove symbolic link

drwxr-xr-x. 2 linux linux 4096 2010-03-20 22:32 Desktop
lrwxrwxrwx. 1 linux linux   16 2010-04-25 17:37 dns -> /etc/resolv.conf
unlink dns


Quote typeNameMeaningExample (type at shell prompt)
"The double quoteThe double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \.Use the double quotes when you want only variables and command substitution.
* Variable - Yes
* Wildcards - No
* Command substitution - yes
The double quotes allowes to print the value of $SHELL variable, disables the meaning of wildcards, and finally allows command substitution.
echo "$SHELL"
echo "/etc/*.conf"
echo "Today is $(date)"
'The single quoteThe single quote ( 'quote' ) protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters.
* Variable - No
* Wildcards - No
* Command substitution - No
The single quotes prevents displaying variable $SHELL value, disabled the meaning of wildcards /etc/*.conf, and finally command substitution ($date) itself. 
echo '$SHELL'
echo '/etc/*.conf'
echo 'Today is $(date)'
\The BackslashUse backslash to change the special meaning of the characters or to escape special characters within the text such as quotation marks.You can use \ before dollar sign to tell the shell to have no special meaning. Disable the meaning of the next character in $PATH (i.e. do not display value of $PATH variable):
echo "Path is \$PATH"
echo "Path is $PATH"
 
  • To see what shell you have, run: echo $SHELL.

    which command

    which bash
    Sample outputs: 
    /bin/bash

    View All System Variables

    set
    OR
    env
    OR
    printenv

    Commonly Used Shell Variables

    The following variables are set by the shell:
    System VariableMeaningTo View Variable Value Type
    BASH_VERSIONHolds the version of this instance of bash.echo $BASH_VERSION
    HOSTNAMEThe name of the your computer.echo $HOSTNAME
    CDPATHThe search path for the cd command.echo $CDPATH
    HISTFILEThe name of the file in which command history is saved.echo $HISTFILE
    HISTFILESIZEThe maximum number of lines contained in the history file.echo $HISTFILESIZE
    HISTSIZEThe number of commands to remember in the command history. The default value is 500.echo $HISTSIZE
    HOMEThe home directory of the current user.echo $HOME
    IFSThe Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>.echo $IFS
    LANGUsed to determine the locale category for any category not specifically selected with a variable starting with LC_.echo $LANG
    PATHThe search path for commands. It is a colon-separated list of directories in which the shell looks for commands.echo $PATH
    PS1Your prompt settings.echo $PS1
    TMOUTThe default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user.echo $TMOUT
    TERMYour login terminal type.echo $TERM
    export TERM=vt100
    SHELLSet path to login shell.echo $SHELL
    DISPLAYSet X display nameecho $DISPLAY
    export DISPLAY=:0.1
    EDITORSet name of default text editor.export EDITOR=/usr/bin/vim

    display the value of a variable with echo $HOME
    echo "$HOME"

    echo "${varName}"

     Define your home directory:
    myhome="/home/v/vivek"
    echo "$myhome"

  • In Linux, the dollar sign ($) stands for a shell variable
  • The ‘echo‘ command just returns whatever you type in.
  • If you get a command wrong, you won’t flunk or damage anything, but it won’t
    work.
  • #!/bin/sh – It is called shebang. It is written at the top of a shell
    script and it passes the instruction to the program /bin/sh.
    Shell script is just a simple text file with “.shextension, having executable
    permission
    chmod 744 hello.sh (making the script executable)

    User can login locally into the console when in runlevel # 3 or graphically when in runlevel # 5 (the level numbers may differ depending on the distribution). In both cases you need to provide username and password
    1. /etc/profile - The systemwide initialization file, executed for login shells.
    2. /etc/bash.bashrc - The systemwide per-interactive-shell startup file. This is a non-standard file which may not exist on your distribution. Even if it exists, it will not be sourced unless it is done explicitly in another start-up file.
    3. /etc/bash.logout - The systemwide login shell cleanup file, executed when a login shell exits.
    4. $HOME/.bash_profile - The personal initialization file, executed for login shells.
    5. $HOME/.bashrc - The individual per-interactive-shell startup file.
    6. $HOME/.bash_logout - The individual login shell cleanup file, executed when a login shell exits.
    7. $HOME/.inputrc - Individual readline initialization file.
    8.  

      Login Shell

      Login shells are first shell started when you log in to the system. Login shells set environment which is exported to non-login shells. Login shell calls the following when a user logs in:
    9. /etc/profile runs first when a user logs in runlevel # 3 (the level numbers may differ depending on the distribution).
    10. $HOME/.bash_profile, $HOME/.bash_login, and $HOME/.profile, runs second when a user logs in in that order. $HOME/.bash_profile calls $HOME/.bashrc, which calls /etc/bashrc (/etc/bash.bashrc).

    Non-Login Shell





    Special bash parameters and their meaning

    Special bash parameterMeaning
    $!$! bash script parameter is used to reference the process ID of the most recently executed command in background.
    $$     $$ is used to reference the process ID of bash shell itself
    $#     $# is quite a special bash parameter and it expands to a number of positional parameters in decimal.
    $0$0 bash parameter is used to reference the name of the shell or shell script. so you can use this if you want to print the name of shell script.
    $-     $- (dollar hyphen) bash parameter is used to get current option flags specified during the invocation, by the set built-in command or set by the bash shell itself. Though this bash parameter is rarely used.
    $?     $0 is one of the most used bash parameters and used to get the exit status of the most recently executed command in the foreground. By using this you can check whether your bash script is completed successfully or not.
    $_$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.
    $@     $@ (dollar at the rate) bash parameter is used to expand into positional parameters starting from one. When expansion occurs inside double-quotes, every parameter expands into separate words.
    $*     $* (dollar star) this is similar to $@ special bash parameter  only difference is when expansion occurs with double quotes, it expands to a single word with the value of each bash parameter separated by the first character of the IFS special environment variable.



    Command example:-  Getting help on Linux null command.
    ., - Execute commands from a file in the current shell.
    Command example:-  Getting help on Linux dot command.
    [, - Evaluate conditional expression. Execute conditional command.
    alias - Print and create command alias.
    Command example:-  Getting help on alias command.
    (1.) List alias commmand - List Linux command that already been alias.
    (2.) Create command alias - Make new command with alias.
    bg - Send job to background.
    Command example:-  Getting help on bg command.  
    bind - Bind a key sequence to a Readline function or a macro, or set a Readline variable.
    Command example:-  Getting help on bind command.  
    break - Exit from loop.
    Command example:-  Getting help on break command.  
    builtin - Execute shell builtin.
    Command example:-  Getting help on Linux builtin command.  
    cd - Change current directory.
    Command example:-  Linux cd command help.
    command - Run builtin command or command found in search path.
    Command example:-  Getting help on command command.  
    compgen - Generate possible completion matches for word.
    Command example:-  Getting help on compgen command.  
    complete - Specify how arguments should be completed.
    continue - Resume next iteration of the enclosing loop.
    Command example:-  Linux continue command help.  
    declare -  Declare variables and give them attributes.
    dirs - Display the list of currently remembered directories.
    Command example:-  Getting help on Linux dirs command.  
    disown - Remove jobs from current shell.
    Command example:-  Getting help on Linux disown command.  
    echo - Display, print, echoing to standard output (screen, files).
    Command example:-  Getting help on Linux echo command.
    enable - Enable and disable builtin shell commands.
    Command example:-  Getting help on enable command
    (1.) List all available Linux built in command using enable command.
    (2.) Step by step disable builtin command by using enable command.
    (3.) Step by step Show disable Linux command by using enable command.
    eval -  Execute arguments as a shell command.
    Command example:-  Getting help on Linux eval command.
    exec -  Execute command.
    Command example:-  Getting help on Linux exec command.
    exit -  Logout or exit from shell.
    Command example:-  Getting help on Linux exit command.
    export - Set an environment variable.
    fc - Display or execute commands from the history list.
    Command example:-  Getting help on Linux fc command.
    fg - Move job to the foreground.
    Command example:-  Getting help on Linux fg command.
    getopts - Parse positional parameters.
    hash - Remember the full pathname of a name argument.
    Command example:-  Getting help on Linux hash command
    help - Display information about builtin commands.
    Command example:-  Getting help on Linux help command.
    history - Command Line history.
    Command example:-  Getting help on history command.
    (1.) Step by step clear Linux command history.
    jobs - Display status of jobs.
    Command example:-  Getting help on Linux jobs command.
    kill - Send kill signal to a job to stop running process.
    Command example:-  Getting help on kill command.
    let - Perform arithmetic on shell variables.
    Command example:-  Getting help on Linux let command.
    local - Create variables.
    Command example:-  Getting help on local command.
    logout - Logout from login shell.
    Command example:-  Getting help on logout command.
    popd - Remove the top entry from the directory stack and restore value of the current directory that popped from the stack.
    Command example:-  Getting help on Linux popd command.
    printf - Format and print data.
    pushd - Save to directory stack and then change the current directory.
    pwd -  Print pathname of current working directory.
    Command example:-  Linux pwd command help.
    (1.) Step by step using Linux pwd command.
    read - Read line from standard input.
    Command example:-  Getting help on Linux read command.
    readarray - Read lines from a file into an array variable.
    readonly - Mark shell variable as unchangeable.
    return - Cause a function to exit.
    set - Set or unset value of shell options and positional parameters.
    Command example:-  Getting help on Linux set command.
    shift - Shift positional parameters.
    shopt - Set and unset shell options.
    Command example:-  Getting help on shopt command.
    source - Execute commands from a file in the current shell.
    suspend - Suspend shell execution.
    test - Evaluate conditional expression.
    Command example:-  Getting help on Linux test command.
    times - Display process times.
    trap - Trap signals and other events.
    Command example:-  Getting help on Linux trap command.
    type - Display information about command type.
    Command example:-  Getting help on type command.
    typeset - Set variable values and attributes.
    ulimit - Modify shell resource limits.
    umask - Display or set file mode mask.
    Command example:-  Getting help on umask command.
    unalias - Remove alias from the list of defined aliases.
    Command example:-  Getting help on unalias command.
    unset - Unset values and attributes of shell variables and functions.
    wait - Wait for job completion and return exit status.
    Command example:-  Linux wait command help.
    excuting java script :- java -jar java.jar

    CharacterWhereMeaning
    $(..)ksh, bashCommand substitution.
    ((..))ksh, bashArithmetic evaluation.
    \. fileshExecute commands from file in this shell.
    :shEvaluate arguments, return true.
    Char.
    Description

    " "
    Whitespace — this is a tab, newline, vertical tab, form feed, carriage return, or space. Bash uses whitespace to determine where words begin and end. The first word is the command name and additional words become arguments to that command.

    $
    Expansion — introduces various types of expansion: parameter expansion (e.g. $var or ${var}), command substitution (e.g. $(command)), or arithmetic expansion (e.g. $((expression))). More on expansions later.

    ''
    Single quotes — protect the text inside them so that it has a literal meaning. With them, generally any kind of interpretation by Bash is ignored: special characters are passed over and multiple words are prevented from being split.

    ""
    Double quotes — protect the text inside them from being split into multiple words or arguments, yet allow substitutions to occur; the meaning of most other special characters is usually prevented.

    \
    Escape — (backslash) prevents the next character from being interpreted as a special character. This works outside of quoting, inside double quotes, and generally ignored in single quotes.

    #
    Comment — an introduction of a # character begins a commentary that extends to the end of the line. Comments are notes of explanation and are not processed by the shell.

    [[]]
    Test — an evaluation of a conditional expression to determine whether it is "true" or "false". Tests are used in Bash to evaluate a number of conditions. More of this will be covered later.

    !
    Negate — used to negate or reverse a test or exit status. For example: ! grep text file; exit $?.

    ><
    Redirection — redirect a command's output or input. Redirections will be covered later.

    |
    Pipe — redirect output from a initial command to the input of secondary command. This is a method of chaining commands together. Example: echo "Hello beautiful." | grep -o beautiful.

    ;
    Command separator — a representation of a newline. Used to separate multiple commands that are on the same line.

    {}
    Inline group — commands inside the curly braces are treated as if they were one command. It is convenient to use these when Bash syntax requires only one command and a function doesn't feel warranted.

    ()
    Subshell group — similar to the above but where commands within are executed in subshell. Used much like a sandbox, if a command causes side effects (like changing variables), it will have no effect on the current shell.

    (())
    Arithmetic expression — with an arithmetic expression, characters such as +, -, *, and / are mathematical operators used for calculations. They can be used for variable assignments like (( a = 1 + 4 )) as well as tests like if (( a < b )). More on this later.

    $(())
    Arithmetic expansion — Comparable to the above, but the expression is replaced with the result of its arithmetic evaluation. Example: echo "The average is $(( (a+b)/2 ))".

    ~
    Home directory — the tilde is a representation of the home directory. When followed by a /, it means the current user's home directory; otherwise, a username will have to be specified (e.g. ls ~/Documents; cp ~john/.bashrc .).
    read -p "Prompt" variable1 variable2 variableN
    
    Where,
    • -p "Prompt" : Display prompt to user without a newline.
    • variable1 : The first input (word) is assigned to the variable1.
    • variable2 : The second input (word) is assigned to the variable2.

    Create an aliase called c to clear the terminal screen, enter:

    To clear the terminal, enter:  

    c

      How do I remove the alias

    unalias alias-name
    unalias c 
    unalias c d
    To list currently defined aliases, enter:
    alias

    How do I permanently add aliases to my session?

    Sample ~/.bashrc file

    • Example ~/.bashrc script:
    • To ignore an alias called ls and run ls command, enter
    • \ls  or "ls"
       

     

       

     

    Handling Input

    Create a script called greet.sh as follows:
    #!/bin/bash
    read -p "Enter your name : " name
    echo "Hi, $name. Let us be friends!"
    
    Save and close the file. Run it as follows:
    chmod +x greet.sh
    ./greet.sh
    
    Sample Outputs:
    Enter your name : Vivek Gite
    Hi, Vivek Gite. Let us be friends!






























    The "\" goes before the special character, not after.
    symbols="!\ #\ $\ %\ &\ '\ (\ )\ *\ +\ ,\ -\ .\ /\ :\ ;\ &\ <\ =\ >\ ?\ @\ [\ \\ ]\ ^\ _\ {\ |\ }\ ~\"

    Job control, including the fg and bg commands and the ability to stop jobs with CTRL-Z.
    • *?[ are special as globbing operators, so only in list contexts. In the case of [, it's [...]that is the globbing operator, either [ or ] only need to be quoted to remove the special meaning.
    • = is special when in contexts where it's treated as an assignment operator. That is, in a simple command, for all words that do not follow an argument (except after set -k).








    Table 1-6. Special characters
    CharacterMeaningSee chapter
    ~Home directory1
    `Command substitution (archaic)4
    #Comment4
    $Variable expression3
    &Background job1
    *String wildcard1
    (Start subshell8
    )End subshell8
    \Quote next character1
    |Pipe1
    [Start character-set wildcard1
    ]End character-set wildcard1
    {Start code block7
    }End code block7
    ;Shell command separator3
    'Strong quote1
    "Weak quote1
    <Input redirect1
    >Output redirect1
    /Pathname directory separator1
    ?Single-character wildcard1
    %Job name/number identifier8

No comments:

Post a Comment