Alias vs function - why is alias buggy?

For discussions about programming and projects not necessarily associated with Porteus.
User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Alias vs function - why is alias buggy?

Post#1 by Rava » 17 Mar 2018, 06:12

My question here: why does alias fx

Code: Select all

alias fx="echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;/bin/free -m"
behave so much different than function fx?

Does anyone of the script coders of Porteus has an answer to that?


I had to replace the alias fx with fx () since it DID NOT UPDATE the time string after suspend; while e.g. the log entries into /var/log/messages reported the correct time AFTER SUSPEND the alias fx started AFTER SUSPEND again did report the same time as the alias fx reported PRIOR SUSPEND.

Here is the code of my suspend-s3b2 (that stands for "sleep 3 seconds, use beep2 script to announce start of suspend"
The first suspend used "beep" script which plays /usr/share/sounds/uget/notification.wav
(Since that sound file is no longer part of porteus I had to copy that into my rootcopy as well)
beep2 replaced the wav file with the same sound, but made more silent by -15 dB using audacity and is /mnt/sda2/sound/notification_-15dB.mp3
I used aplay in "beep" for playing the wav file but had to change to mpg123 in "beep2" since aplay is only able to play wav files and cannot play ogg or mp3 files (which is a bit strange to me, since aplay is meant to test the sound card or linux sound a5rchitecture… and why only use wav files for that when some people would have longer / more elaborate test files in compressed form like ogg/vorbis or .mp3…
Anyhow, aplay vs whatever console mp3/ogg-vorbis player is not the issue here.

Just know that beep2 will not be on your system so instead of playing a short notification sound you would just get an error message.
And Cave! As the script name says, the script sends the Porteus box into suspend, reporting the start of suspend into the main log file of /var/log/messages, calculating the time in suspend using the function seconds2time () and reporting the result both into console / terminal and also into the log file, and reporting the end of suspend into the log as well.
Also it gives both the actual time and "free -m" prior and after suspend using the function fx ()

(And it uses my own file containing the escape codes for colours and bold as defined in the string "$ECHO_COLORS" to address the file; not having that file and not having $ECHO_COLORS defined is no issue the output just lacks any colours)

Code: Select all

#!/bin/bash
VERSION="0.1"
MYNAME="suspend-s3b2"

test ! "$ECHO_COLORS"x = "x" && test -f $ECHO_COLORS && . $ECHO_COLORS

echo -e "${bld}${yel}$MYNAME$off V$yel$VERSION${off}"
echo "(sleep 3s; beep2)"

# aliases and functions
function fx ()
{
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________
    /bin/free -m
}

function seconds2time () 
{
    local T=$1;
    local D=$((T/60/60/24));
    local H=$((T/60/60%24));
    local M=$((T/60%60));
    local S=$((T%60));
    if [[ ${D} != 0 ]]; then
        if [[ ${D} = 1 ]]; then
            printf '%d day %02d:%02d:%02d' $D $H $M $S;
        else
            printf '%d days %02d:%02d:%02d' $D $H $M $S;
        fi;
    else
        printf '%02d:%02d:%02d' $H $M $S;
    fi
}

# main
sleep 3s
beep2
fx
declare -i startepoch=$(date +%s)
logger '●●●●● before suspend ●●●●●●●●●●●●●●●'
pm-suspend
echo '●●●●● pm-suspend took '$(seconds2time $(date +%s)-$startepoch)' … ●●●●●●●●●●●●●●●'|tee -a /var/log/messages
logger '●●●●● after suspend ●●●●●●●●●●●●●●●'
fx
Cheers!
Yours Rava