2

So, I recently downloaded a cli which shares a lot of commands similar to linux. Like mega-login : login & mega-logout :logout and I am too tired of writing mega every time I need to use the cli. I thought of using alias but I don't want to screw some other program that use the some other command I don't know of. Like if I create an alias login then what will happen to the system login command? Plus there are a lot of commands in the cli.

So, I thought of using a script, but I didn't know how to do it. Here what I had in my mind. So when I use m login or m logout it will search if a command mega-login or mega-logout exists and then call it.

So I tried hours coming up with various functions while learning fish and this is what I wrote.

function m --argument value $argv
    echo value $value $argv
    if type mega-$value
        set MEGA
        for temp in $argv
            set MEGA $MEGA$temp
            echo $MEGA
        end
        echo MEGA \n\n $MEGA
        eval $MEGA
    end
end

This still doesn't work. PS: I wrote this on my first try, but I dropped it once I couldn't figure it out.

function m --argument value $argv
    echo value $value $argv
    if type mega-$value
        set MEGA mega-$argv
        echo $argv \n\n\n $MEGA
    eval $MEGA
    end
end
1
  • 1
    I removed the "SOLVED" tagging of the title of the question. Accepting an answer is enough to mark a question as resolved.
    – Kusalananda
    Jan 18, 2022 at 18:01

1 Answer 1

1

Not a bad first (and second) attempt. It can be tricky to get it just right.

How about:

function m
    set -l mega_cmd "mega-$argv[1]"
    set -l mega_args $argv[2..]
    if type -q "$mega_cmd"
        echo "Executing $mega_cmd $mega_args"
        "$mega_cmd" $mega_args
    end
end

Of course, that's for readability (which I prefer), but it can be condensed down to:

function m
    if type -q "mega-$argv[1]"
        echo "Executing mega-$argv[1] $argv[2..]"
        "mega-$argv[1]" $argv[2..]
    end
end
8
  • Random thoughts for improvements -- If you really want to keep the "Executing ..." in there, probably better to send it to stderr so as to not pollute any output results from your actual mega-x commands. Also, of course, would be good to have an else block to provide an error message if the mega-x command isn't found. Jan 18, 2022 at 17:34
  • It helped me a lot, I was going to abbr every command if I couldn't find any way. Jan 18, 2022 at 17:38
  • Great to hear that helped. Don't forget to upvote and accept the answer. And check back, because someone else may have an even better solution. And if that happens, I want you to have the opportunity to accept theirs instead! Thanks! Jan 18, 2022 at 17:46
  • This will fail if only a function of that name exists. type -q checks for all kinds of callable things - functions, builtins and commands. And then you attempt to just run an external command with command. My preference would be to just remove the command entirely. (also most of the quoting here is superfluous)
    – faho
    Jan 19, 2022 at 9:15
  • Thanks @faho All good points. I removed command from the function. Any idea why the commandline editor then flags the "$mega_cmd" (quoted or unquoted, it doesn't matter) as an error? It still works; just that it is highlighted in red in the editor. Also, I realized that quoting the command variable was overkill, but I'm just trying to maintain "good variable quoting" habits. It would take a pathologic command (with a space in the command itself) to break the unquoted version, I know. There's no potential harm in leaving it quoted, though, right? Jan 19, 2022 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .