bash script

How to remove the Nth line from a file

Easy solution with sed:

$ sed --in-place '3d' test.txt

konsole and bash script: attracting user attention when input is needed

How to have a script run in a console tab, and attract the user attention when the script needs user input?

Use case:
in my scripts, I have sudo commands. When the last sudo ran a short while ago, it goes without a hitch. But sometimes, the script is stuck at the password prompt and I need a way to attract the user attention so that he can enter the password.

I can echo a bell just before the sudo command but:
1- the bell will ring whether or not user interaction is needed (sudo may not need user input).
2- the speakers may not be on.

bash: variable scope

See the use of the keywords local and global, the switch -G for declare.

Late evaluation: expressions are evaluated at the last moment.

What is the scope of local? A code flow segment (between { and }, like C++) or a function?

bash arrays: keys (indices) and values

List all the elements in an array:
${my_array[@]}

List all the keys (indices) in an array:
${!my_array[@]}

Find if an index/key exist

if test "${my_array['key']+isset}"
    then
        echo "The key exists."
    else
        echo "The key does not exist."
fi

Bash: iterate over bash array in the order the elements were declared.

When I iterate over a map (associative array), the order in which the elements in the array are iterated depend on the type of keys.

If my keys are 1, then 'b' (defined in that order), I get them back in the same order: 1, then 'b'.
If my keys are 1, then 2 (defined in that order), I get them in reverse order: 2, then 1.
This is without changing the for loop, nor changing the order in which the elements are declared, simply changing the keys.

bash function return value

function my_function {
    echo "Return value."
}

result=$(my_function)

return statement

Beware! Do not use the return statement to return a value. The return statement is used to return exit codes, usually 0 for a normal exit, and 1 if an error occurred within the function. Instead, use the format above.

From help return:

return: return [n]

Return from a shell function.

Causes a function or sourced script to exit with the return value
specified by N. If N is omitted, the return status is that of the

bash: debugging

Check the use of:

set -o verbose
set -v
set -x

Bash: pipe |

Commands used with a pipe | are run in a sub-shell, with the same pitfalls as using parenthesis might have:
http://linux.overshoot.tv/wiki/bash_parenthesis

This will not give the expected result, which is to abort at the first wrong line:

#!/bin/bash

cat file.txt | while read line
do
    if [ there_is_something_wrong_with_the_line ]; then
       exit 1
    fi
    do_something
done

Use input redirection instead:


#!/bin/bash

while read -r line
do
if [ there_is_something_wrong_with_the_line ]; then
exit 1
fi
do_something

Bash: parenthesis ()

Parenthesis in bash start a sub-shell.

See the following page for one example of some unexpected consequence of using a sub-shell:
http://linux.overshoot.tv/wiki/bash_exit

Syndicate content