bash script

How to stream the output of a command in a bash script?

Adding a command in a script will simply print the command output to the console.
But what if I want to filter the output, add to it, act upon it, etc.?

Right now, I simply assign the output of the command to a variable, print the variable and grep the variable to find the bits that interest me.
But if the command takes a long time to run, the user won't see any output until the command has finished running.
I want to do all of the above, while the user is seeing the output and the process progresses...

bash: switch case flow control

case $var in
    pattern1 )
        statements ;;
    pattern2 )
        statements ;;
    *)
        statemest ;;
esac

Note the two consecutive semi-column ";;" which are a necessary part of the syntax!
The *) case is the default choice if none other apply.

#!/bin/bash
format() {
        case "$1" in
        'banana' | 'apple' )
                echo 'It is a fruit.';;
        'cat' )
                echo 'It is an animal.';;
        esac
}

print_category "banana"
print_category "cat"
print_category "apple"

bash: working with strings

string length:
${#string}

${string:position}
${string:position:length}

${string%substring}
shortest:
${filename#*/}
${filename#/*}
longest:
${filename##*/}
${filename##/*}

#!/bin/bash

# E.g. PWD = "/home/user/scripts/myproject"
PWD=`pwd`
project=${PWD##*/}
#$project = "myproject"
echo "$project"

${string/pattern/replacement}

Bash script: how does the $ wildcard work in git script?

I checked that the following is true, but I still don't understand why or how it works:

#!/bin/bash

git checkout production.release.4.2
production_branch=`git branch | grep "production"`

# May not give the expected result:
# the character $ is being treated as a wildcard and will be expanded, so that the output may include all files in the directory:
echo $production_branch
# Using double quotes solve the problem:
echo "$production_branch"

git: git branch name programmatically

In a bash script, you can get the current branch's name programmatically thus:

#!/bin/bash

branch_name=$(git symbolic-ref -q HEAD)
echo $branch_name
branch_name=${branch_name##refs/heads/}
echo $branch_name
branch_name=${branch_name:-HEAD}
echo $branch_name

Beware, to find a branch name (whether checked out or not) according to a pattern:


#!/bin/bash

git checkout production.release.4.2
production_branch=`git branch | grep "production"`

# May not give the expected result:

bash: validating a file (existence, type...)

How to validate a file?

#!/bin/bash
FILE=$1

if [ -f $FILE ];
then
   echo "The file $FILE exists."
fi

# For a directory, use -d:

if [ ! -d $FILE];
then
    echo "The file $FILE is not a directory."
fi

For more information, see section "6.4 Bash Conditional Expressions" of the bash documentation:
http://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expre...

bash: working with files

See below for a series of wiki pages about working with files within a bash script.

bash: iterate over files in directory

Here is a sample code:

#!/bin/bash
FILES=~/my_directory/*

for file in $FILES
do
  # Do something with your file $file
  cat $f
done

Bash: write (echo) to stderr

3.6.4 Redirecting Standard Output and Standard Error
http://www.gnu.org/software/bash/manual/bash.html#Redirecting-Standard-O...

#!/bin/bash
# Both are OK:
echo "WARNING! My error message." 1&>2
1&>2 echo "WARNING! My error message."

Note that &>2 is the same as 1&>2 since 1 is stdout and is the default output channel.

Or you could define a function:

#!/bin/bash
print_error() {
   echo "$@" 1>&2;
}

print_error "WARNING! My error message."

Syndicate content