[TriLUG] Single line scripts

Lee Fickenscher via TriLUG trilug at trilug.org
Tue Sep 6 00:47:58 EDT 2016


I'll third aliases/functions and also add that you should check out "man
ssh_config" for those oft-used unwieldy ssh connections.

-Lee

On Mon, Sep 5, 2016 at 9:07 PM, Brian Gerard via TriLUG <trilug at trilug.org>
wrote:

> On 09/05/2016 03:34 PM, Grawburg via TriLUG wrote:
> >
> > I have long commands I enter in a terminal, such as starting
> > connecting to a Raspberry Pi located several floors away with ssh, or
> > setting up a connection to my NAS (rather than having a line in
> > /etc/fstab).
> > I can simply have a text file saved and do a copy and paste into
> > terminal each time I need it, but I'd like to turn it into a what I
> > would have called a .bat file decades ago.
> > Is this something I should learn to to do in bash? I don't need
> > multiple lines, there is no extra coding, just a single line.
> >
> > Thanks,
> > Brian Grawburg
>
> Going by your description, I would second the recommendation for aliases
> or functions.  The decision between them will more or less come down to
> whether or not you need to pass arguments to the commands.
>
> For example...
>
> #
> # Use an alias if the command is exactly the same every time.
> #
> bash$ alias dfhome='ssh -q my-home-machine "df -h"'
> bash$ dfhome
> Filesystem      Size  Used Avail Use% Mounted on
> /dev/sda1       312G  112G  185G  38% /
> tmpfs           385M  896K  384M   1% /run
> /dev/sda2        30G  179M   28G   1% /tmp
> /dev/sda3       184G   65G  110G  38% /home
>
> #
> # Or a function if you want to be able to change behavior.
> #
> bash$ function dfon() { ssh -q $1 "df -h"'; }
> bash$ dfon my-home-machine
> Filesystem      Size  Used Avail Use% Mounted on
> /dev/sda1       312G  112G  185G  38% /
> tmpfs           385M  896K  384M   1% /run
> /dev/sda2        30G  179M   28G   1% /tmp
> /dev/sda3       184G   65G  110G  38% /home
> bash$ dfon some-other-machine
> # ...etc...
>
> Try it, tweak it, repeat.   Once you've got it where you want it, add it
> to your .bashrc and you'll have it whenever you log in.
>
> A shell script in your own ~/bin directory or the like is absolutely
> fine as well, but sounds a trifle heavy for what you've described.
> However, I would say the opposite if these are things you'll need to ssh
> in and run without starting up a login shell (like 'ssh my-host
> "one-of-these-commands"').  In that case you will want a separate shell
> script rather than aliases or functions in your .bashrc.
>
> I covered some of this in the "bash tips n tricks" talk I gave a couple
> of years back.  You can find the materials for that at:
> https://github.com/briangerard/bash_class
> (the section on aliases begins on slide 32, and functions begin on
> slide 38)
>
> ...and the recording is viewable here:
> https://www.youtube.com/watch?v=J4_tFm4iRpc
>
> HTH-
> Brian
>
>
> --
> This message was sent to: elfick at gmail.com <elfick at gmail.com>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web  : http://www.trilug.org/mailman/
> options/trilug/elfick%40gmail.com
> Welcome to TriLUG: http://trilug.org/welcome
>


More information about the TriLUG mailing list