help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: alias not working


From: alex xmb ratchev
Subject: Re: alias not working
Date: Wed, 26 Apr 2023 20:07:21 +0200

On Wed, Apr 26, 2023, 20:06 alex xmb ratchev <fxmbsw7@gmail.com> wrote:

>
>
> On Wed, Apr 26, 2023, 19:34 Greg Wooledge <greg@wooledge.org> wrote:
>
>> On Wed, Apr 26, 2023 at 12:21:09PM -0500, Mike McClain wrote:
>> > I have a non-standard calender file with entries like this:
>> >
>> > May 5 '23       Fri 1900    next TASCA dance
>> >
>> > May 6 '23       Sat 1800    Cherokee Co. barn dance, CC Expo
>> >
>> > I have an alias 'mo' defined as so:
>> >
>> > alias mo='m=$(command date "+%b");
>> >     echo; echo Today is $(command date "+%a %-d %b. %Y %-k:%M");
>> >     if [ -z "$1" ]; then
>> >         egrep "^$m" calendar;
>> >     else
>> >         egrep "^$1" calendar;
>> >     fi'
>>
>> Aliases don't take arguments.  "$1" inside this alias will simply be
>> the script's $1, or your interactive shell's $1, which is very likely
>> the empty string.
>>
>> What you want is a function.  Functions can be given arguments, which
>> are accessed as positional parameters ($1 and so on).
>>
>> unalias mo
>> mo() {
>>     printf '\nToday is %s\n' "$(date '+%a %-d %b. %Y %-k:%M')"
>>     if [ -z "$1" ]; then
>>         local m=$(date +%b)
>>         egrep "^$m" calendar
>>     else
>>         egrep "^$1" calendar
>>     fi
>> }
>>
>
> egrep "^${1:+$m}" .file
>

sorry not :+
:-

egrep "^${1:-${m:-$( date .. )}}" .file


> I'll also point out, for the record, two improvements that can be made
>> here.
>>
>> 1) The printf command can generate timestamps without needing to call
>>    date(1).  I'm not sure if all of your date arguments have corresponding
>>    printf %(...)T specifiers, but generally speaking you can get most
>>    of what you want without calling date.
>>
>> 2) Calling date twice in the same function/script can lead to different
>>    results, if time passes between the two calls, or if the system clock
>>    jumps forward, or if you run the program right before midnight, etc.
>>    It's best to get all of the fields you need in a single call to date
>>    (or printf %()T), and parse them out as needed.
>>
>>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]