Search code examples
bashfunctionparadigms

What are the programming paradigms used in the bash language?


In a bunch of languages I tried, functions can return values in a fair amount of types (ints, strings, various objects, functions that return functions etc.), but in bash, it seems like the only type allowed is int, and it is not supposed to be used by other functions but by the shell itself to know if everything went OK (returning 0), or if some error occured, in which case the value returned is the error code.

So for example, suppose you want to write a function that want to return something to another function. How should you do it ?

Suppose you want to write a findvideos function that returns every video found in a particular folder (and its subfolders). You want to use the result of that function in another function, how would you do it ? via echo and pipes ?

I was stuck with while writing a small script like this one :

function firefoxcache {
    cache_dir=$1
    videos_dir=$2
    videos=$(findallvideos $videos_dir)
    for video in $videos; do
    echo cp $video $videos_dir/$(basename $video).flv
    done;
}

function findallvideos {
    videos=$(find $1 -exec file {} \; | grep -i $VIDEOS_REGX | cut -f1 -d:)
    #echo $videos
    return videos
}   

EDIT

With help of Stackers, here's the working code (minus the silly double slash problem):

#!/usr/bin/env bash
function firefoxcache {
cache_dir=$1
videos_dir=$2
echo "$cache_dir", "$videos_dir"
echo
for video in $(findallvideos $cache_dir); do
echo cp $video $videos_dir/$(basename $video).flv
cp $video $videos_dir/$(basename $video).flv
done;
}

function findallvideos {
#echo findallvideos "$1"
for video in $(find $1 -exec file {} \; | grep -i $VIDEOS_REGX | cut -f1 -d:); do
echo $video;
done;
}

firefoxcache $1 $2

# cp /home/chaouche/.cache/mozilla/firefox/qmkhe4mr.default/Cache/4/B6/6AB61d01 VIDEOS//6AB61d01.flv
# cp /home/chaouche/.cache/mozilla/firefox/qmkhe4mr.default/Cache/4/28/A3524d01 VIDEOS//A3524d01.flv
# cp /home/chaouche/.cache/mozilla/firefox/qmkhe4mr.default/Cache/4/D2/29D4Ad01 VIDEOS//29D4Ad01.flv
# cp /home/chaouche/.cache/mozilla/firefox/qmkhe4mr.default/Cache/4/A1/15D75d01 VIDEOS//15D75d01.flv
# cp /home/chaouche/.cache/mozilla/firefox/qmkhe4mr.default/Cache/4/A3/2C971d01 VIDEOS//2C971d01.flv
# cp /home/chaouche/.cache/mozilla/firefox/qmkhe4mr.default/Cache/4/7E/E17D8d01 VIDEOS//E17D8d01.flv
# cp /home/chaouche/.cache/mozilla/firefox/qmkhe4mr.default/Cache/4/AC/5295Dd01 VIDEOS//5295Dd01.flv

Solution

  • Shell scripts can only return integer values from function types. If you want string output from them you have to echo the string and then capture that output within a subshell assigned to a variable.

    e.g.

    function foo()
    {
        if [ "$1" = "hello" ] ; then
            echo "world";
            return 0;
        fi
        return 1;
    }
    
    a=$(foo "hello"); # a = world
    echo $?;  # prints 0 - return values and exit status's are always assigned here.
    
    a=$(foo); # a = empty string
    echo $?;  # prints 1.