1

I have to do an alias called for example "longest" for this script:

data=""; len=0; line=""; while [[ $line != "quit" ]]; do read line; [[ $line != "quit" ]] && [[ ${#line} -gt len ]] && len=${#line} data=$line; done; echo $len; echo $data 1>2

Its job is simply reading words or phrases and counting the characters.

I put the script inside quotes like this:

alias longest="...script..."

but it doesn't work and I don't know why. Can anyone explain to me why? A possible solution?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
AleC
  • 13
  • 4
  • 2
    Why dont you simply write a script, make it executable and call it? – Michail Alexakis May 15 '22 at 18:45
  • 1
    This absolutely should not be an alias. But, as a first attempt, try just using single quotes: `alias longest=' ... '` – William Pursell May 15 '22 at 19:02
  • 5
    That should be a function. – Shawn May 15 '22 at 19:03
  • 1
    Did you really mean to send output to a file named "2"? If you're trying to send output to stderr, use `1>&2` (or just `>&2`) rather than `1>2`. Also, there are a bunch of things in there that really should be double-quoted to avoid parsing weirdness. Finally, I'd recommend stopping on end-of-file as well as/instead of when getting "quit" (you could use `while read line && [[ $line != "quit" ]]; do ...` to check for either). – Gordon Davisson May 15 '22 at 20:46

1 Answers1

2

You have several options (and I'm repeating the script from the question, not fixing any other errors (?) like redirecting to a file with name 2):

  1. Have the alias define a function and execute it immediately

    alias longest='f() { data=""; len=0; line=""; while [[ $line != "quit" ]]; do read line; [[ $line != "quit" ]] && [[ ${#line} -gt len ]] && len=${#line} data=$line; done; echo $len; echo $data 1>2; }; f'
    
  2. Create a script and save it in a directory from your PATH, usually ~/bin:

    File ~/bin/longest:

    #!/bin/bash
    data="";
    len=0;
    line="";
    while [[ $line != "quit" ]]; do
       read line;
       [[ $line != "quit" ]] && [[ ${#line} -gt len ]] && len=${#line} data=$line;
    done;
    echo $len;
    echo $data 1>2
    

    and finally chmod +x ~/bin/longest.

  3. Define the function in your .bashrc file and then call it on demand.

  4. Avoid the complicated, home-grown code and go for something much simpler. The behavior and output will not be identical, but should be sufficient.

    alias longest="awk '{print length, \$0}' | sort -nr | head -1"
    
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 2
    Why wrap a function in an alias instead of just defining & using the function it directly? Wrapping it in an alias just seems like useless complexity (& opportunity for parsing confusion). Speaking of which, in the `awk` version, you need to escape the `$` to keep `$0` from being expanded (since it's in double-quotes as the alias is being defined). – Gordon Davisson May 15 '22 at 20:43
  • @GordonDavisson thanks for the hint, I have fixed the awk example. As for why a function in an alias? Because the question was asking for a way to do this with an alias. I provided other options to suggest different solutions that do not necessarily involve a (complicated) alias. – knittl May 16 '22 at 18:11