Pages

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[@]}