Pages

Thursday, July 15, 2021

Select command in bash

 #!/bin/bash

# Define the menu list here

select brand in Samsung Sony iphone symphony Walton

do

echo "You have chosen $brand"

done


#!/bin/bash

echo "Which Operating System do you like?"


# Operating system names are used here as a data source

select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP

do


case $os in

# Two case values are declared here for matching

"Ubuntu"|"LinuxMint")

echo "I also use $os."

;;

# Three case values are declared here for matching

"Windows8" | "Windows10" | "WindowsXP")

echo "Why don't you try Linux?"

;;

# Matching with invalid data

*)

echo "Invalid entry."

break

;;

esac

done

find difference between ; and +

Difference between {} \; and {} +


find . -exec grep chrome {} \;

or

find . -exec grep chrome {} +


find will execute grep and will substitute {} with the filename(s) found. The difference between ; and + is that with ; a single grep command for each file is executed whereas with + as many files as possible are given as parameters to grep at once.


find . -exec grep chrome {} +

find will execute grep and will substitute {} with the filename(s) found. The difference between ; and + is that with ; a single grep command for each file is executed whereas with + as many files as possible are given as parameters to grep at once.

Wednesday, July 14, 2021

Ways to check reachability of remote ports

A port is a logical entity which acts as a endpoint of communication associated with an application or process on an Linux operating system. It is useful to know which ports are open and running services on a target machine before using them.


We can easily list open ports in Linux on a local machine using the netstat or several other Linux commands such NMAP.


In this guide, we will show you how to determine if ports on a remote host are reachable/open using simple netcat (in short nc) command.


netcat (or nc in short) is a powerful and easy-to-use utility that can be employed for just about anything in Linux in relation to TCP, UDP, or UNIX-domain sockets.


# yum install nc                  [On CentOS/RHEL]

# dnf install nc                  [On Fedora 22+]

$ sudo apt-get install netcat     [On Debian/Ubuntu]

We can use it to: open TCP connections, listen on arbitrary TCP and UDP ports, send UDP packets, do port scanning under both IPv4 and IPv6 and beyond.


Using netcat, you can check if a single or multiple or a range of open ports as follows. The command below will help us see if the port 22 is open on the host 192.168.56.10:


$ nc -zv 192.168.1.15 22

In the command above, the flag:


-z – sets nc to simply scan for listening daemons, without actually sending any data to them.

-v – enables verbose mode.

The next command will check if ports 80, 22 and 21 are open on the remote host 192.168.5.10 (we can use the hostname as well):

nc -zv 192.168.56.10 80 22 21


It is also possible to specify a range of ports to be scanned:’


$ nc -zv 192.168.56.10 20-80

Tuesday, July 13, 2021

builtin(command) bash

builtin command is useful to define a shell function with the same name as a shell builtin, retaining the functionality of the builtin within the function.


 The main use of this command is to define a shell function having the same name as the shell builtin by keeping the functionality of the builtin within the function.


Syntax:


builtin [shell-builtin [arg ..]]

Example: Here, we are creating a function to replace the ‘cd’ command. When you will use cd() function it will change the directory to the desktop directly.

Saturday, May 15, 2021

C style for loop

The syntax of the C-style for loop is taking the following form:

    for ((INITIALIZATION; TEST; STEP))

        do

        [COMMANDS]

    done


The INITIALIZATION part is executed only once when the loop starts. Then, the TEST part is evaluated. If it is false, the loop is terminated. If the TEST is true, commands inside the body of the for loop are executed, and the STEP part is updated.

In the following example code, the loop stars by initializing i = 0, and before each iteration checks if i = 10. If true it prints the current value of i and [increments the variable] i by 1 (i++) otherwise the loop terminates.


    for ((i = 0 ; i <= 1000 ; i++)); do

          echo "Counter: $i"

    done

The loop will iterate 1001 times.

Friday, May 14, 2021

typeset and declare

In bash, typeset and declare are exactly the same. The only difference is that typeset is considered obsolete.


typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...

    Set variable values and attributes.

    Obsolete.  See `help declare'.

The man page even lists them in the same breath:


'typeset'

typeset [-afFrxi] [-p] [NAME[=VALUE] ...]

The 'typeset' command is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of the 'declare' builtin command.


declare [-aAfFgilrtux] [-p] [name[=value] ...]

typeset [-aAfFgilrtux] [-p] [name[=value] ...]

    Declare variables and/or give them attributes.

typeset is portable to some other shells, for example, ksh93. If you are aiming for cross-shell portability, use typeset (and make sure that the way you are calling it is portable). If you don't care about such portability, use declare.

Difference between typeset and declare:

The typeset is more portable(e.g. ksh), while the declare is more preferable when portability is not a concern.

Difference between declare(or typeset) and local when used inside a function:

The typeset implies the declare, but more powerful. For example, declare -i x makes x have the integer attribute, declare -r x makes x readonly, etc.

local and declare are mostly identical and take all the same arguments with two exceptions: local will fail if not used within a function, and local with no args filters output to print only locals, declare doesn't.



ssh user@host "$(declare -f foo);foo"


#!/bin/bash

# Define your function

myfn () {  ls -l; }

To use the function on the remote hosts:


typeset -f myfn | ssh user@host "$(cat); myfn"

typeset -f myfn | ssh user@host2 "$(cat); myfn"

Better yet, why bother with pipe:


ssh user@host "$(typeset -f myfn); myfn"

Or you can use a HEREDOC:


ssh user@host << EOF

    $(typeset -f myfn)

    myfn

EOF

If you want to send all the functions defined within the script, not just myfn, just use typeset -f like so:


ssh user@host "$(typeset -f); myfn"

Explanation

typeset -f myfn will display the definition of myfn.

cat will receive the definition of the function as a text and $() will execute it in the current shell which will become a defined function in the remote shell. Finally the function can be executed.

The last code will put the definition of the functions inline before ssh execution.




Tuesday, May 11, 2021

mapfile

mapfile also called (read array) is a command of the Bash shell used to read arrays. It reads lines from the standard input into an array variable. Also, mapfile must read from substitution (< <) and not from a pipe. 

Only available from bash 4.

Also, mapfile is faster and more convenient when compared to a read loop. It returns 1 or 0 depending on whether the command was successful or not. If no array name is not specified, the default variable MAPFILE is used as the target array variable.

Syntax: mapfile [array]

Alternatively, we can use read array [arrayname] instead of mapfile.

Example 1. Reading an array from a file:

$ mapfile MYFILE < example.txt

$ echo ${MYFILE[@]}

$ echo ${MYFILE[0]}

Example 2. Capture the output into an array:

$ mapfile TESTARRAY < <(printf "Item 1\nItem 2\nItem 3\n")

$ echo  ${TESTARRAY[@]}

Here, Item1, Item2, and Item 3 have been stored in the array TESTARRAY.

Example 3. Strip newlines and store item using -t:

$ mapfile -t TESTARRAY< <(printf "Item 1\nItem 2\nItem 3\n")

$ printf "%s\n" "${TESTARRAY[@]}"

$ mapfile -n 2 TESTARRAY < example.txt

$ echo  ${TESTARRAY[@]}

Monday, April 12, 2021

About BASH getopts

getopts is the bash version of another system tool, getopt. Notice that the bash command has an s at the end, to differentiate it from the system command.

Advantages

  • It can handle things like -xvf filename in the expected Unix way, automatically.
  • It makes sure options are parsed like any standard command (lowest common denominator), avoiding surprises.
  • Disadvantages
  • It cannot handle options with optional arguments à la GNU.
  • Options are coded in at least 2, probably 3 places -- in the call to getopts, in the case statement that processes them, and in the help/usage message that documents them.
  • Note that getopts is neither able to parse GNU-style long options (--myoption) nor XF86-style long options (-myoption). So, when you want to parse command line arguments in a

Syntax

getopts optstring optname [ arg ]

Usage


- if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by whitespace. The colon (‘:’) and question mark (‘?’) may not be used as option characters

Note

There are two reserved characters which cannot be used as options: the colon (":") and the question mark ("?").

How it Works

getopts processes the positional parameters of the parent command. In bash, this is stored in the shell variable "$@". So, if you run this command:

mycmd -a argument1 -b argument2

During the time that mycmd is running, the variable $@ contains the string "-a argument1 -b argument2". You can use getopts to parse this string for options and arguments.

Every time you run getopts, it looks for one of the options defined in optstring. If it finds one, it places the option letter in a variable named optname. If the option does not match those defined in optstring, getopts sets variable optname to a question mark ("?").

If the option is expecting an argument, getopts gets that argument, and places it in $OPTARG. If an expected argument is not found, the variable optname is set to a colon (":"). Getopts then increments the positional index, $OPTIND, that indicates the next option to be processed.

The special option of two dashes ("--") is interpreted by getopts as the end of options.

getopts is designed to run multiple times in your script, in a loop, for example. It processes one option per loop iteration. When there are no more options to be processed, getopts returns false, which automatically terminates a while loop. For this reason, getopts and while are frequently used together.

Specifying the optstring

optstring is a string which defines what options and arguments getopts look for. For instance, in this call to getopts:
getopts "apZ" optname
The options expected by getopts are -a, -p, and -Z, with no arguments. These options can be combined in any order as -aZ, -pa, -Zap, etc.

Let's say that you'd like the -a and -Z options to take arguments. You can specify this by putting a colon (":") after that option in optstring. For example:
getopts "a:pZ:" optname

Now you can specify arguments to the -a and -Z options such as -a argument1 -pZ argument2. The -p option cannot take arguments, because there is no colon after the p in optstring.

Error checking

By default, getopts will report a verbose error if it finds an unknown option or a misplaced argument. It also sets the value of optname to a question mark ("?"). It does not assign a value to $OPTARG.

If the option is valid but an expected argument is not found, optname is set to "?", $OPTARG is unset, and a verbose error message is printed.
Silent error checking

However, if you put a colon at the beginning of the optstring, getopts runs in "silent error checking mode." It will not report any verbose errors about options or arguments, and you need to perform error checking in your script.

In silent mode, if an option is unexpected, getopts sets optname to "?" and $OPTARG to the unknown option character.

If the option is OK but an expected argument is not found, optname is set to a colon (":") and $OPTARG is set to the unknown option character.
Specifying custom arguments
You will usually want getopts to process the arguments in $@, but in some cases, you may want to manually provide arguments for getopts to parse. If so, you can specify these args as the final argument of the getopts command.

Examples

Here is a bash script using getopts. The script prints a greeting, with an optional name, a variable number of times. It takes two possible options: -n NAME and -t TIMES.

removes n strings from the positional parameters list. Thus shift $((OPTIND-1)) removes all the options that have been parsed by getopts from the parameters list, and so after that point, $1 will refer to the first non-option argument passed to the script.
Update

As mikeserv mentions in the comment, shift $((OPTIND-1)) can be unsafe. To prevent unwanted word-splitting etc, all parameter expansions should be double-quoted. So the safe form for the command is

Note that $OPTIND is a shell variable managed by the getopts builtin command of the shell. When the shell starts, $OPTIND equals 1 and is incremented every time a getopts call discoveres options or option arguments.

he exact use of shift $(($OPTIND-1)) is to skip the options parsed and standardized by getopts (i.e. -uvh will become -u -v -h), so by doing so, you will have your first "non-option" argument in the $0 position of the argument array.

shift "$((OPTIND-1))"
getopts is used by shell scripts to parse positional parameters. optstring contains the option characters to be recognized; if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by whitespace. The colon (‘:’) and question mark (‘?’) may not be used as option characters. Each time it is invoked, getopts places the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND. OPTIND is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable OPTARG. The shell does not reset OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.

When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-option argument, and name is set to ‘?’.
shift n

removes n strings from the positional parameters list. Thus shift $((OPTIND-1)) removes all the options that have been parsed by getopts from the parameters list, and so after that point, $1 will refer to the first non-option argument passed to the script.

As mikeserv mentions in the comment, shift $((OPTIND-1)) can be unsafe. To prevent unwanted word-splitting etc, all parameter expansions should be double-quoted. So the safe form for the command is
shift "$((OPTIND-1))"

getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead.
OPTIND is initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable OPTARG
While the getopt system tool can vary from system to system, bash getopts is defined by the POSIX standard. So if you write a script using getopts, you can be sure that it runs on any system running bash in POSIX mode (e.g., set -o posix).
getopts parses short options, which are a single dash ("-") and a letter or digit. Examples of short options are -2, -d, and -D. It can also parse short options in combination, for instance -2dD.
However, getopts cannot parse options with long names. If you want options like --verbose or --help, use getopt instead.

Difference with getopt
Never use getopt(1). getopt cannot handle empty arguments strings, or arguments with embedded whitespace. Please forget that it ever existed.

Positional And Optional Argument

Positional And Optional Argument

positional and optional arguments. The positional arguments must occur at the location specified (ex: $1, $2) while the optional arguments can occur at any position denoted by their command line flag

Saturday, March 20, 2021


Command in script

awk '{print >> ( $3 ".txt")}' filename
Input file

sample_text1 text3 20190426
sample_text2 text4 20190426
text1 abc 20190425
text2 def 20190425
generated output's (20190426.txt and 20190425.txt)

20190426.txt

sample_text1 text3 20190426
sample_text2 text4 20190426

20190425.txt

text1 abc 20190425
text2 def 20190425


You can change print to print $1 – steeldriver Apr 27 '19 at 1:41
i want to create output file based on third fields but in output can we have only $1 – upkar Apr 27 '19 at 1:42    


Got the answer now. As the 3rd field holds the essential part of the target file name, the latter is contructed from it and then used for the redirection of the print statement, which by default prints $0, the entire input line