0

Coding is one of my weaker areas and this is my first question on Stack Overflow.

What I want to is add a parameter in-between my command, and I'm thinking it can be done with an alias or function.

The command I am using is telnet and it is used to log into our switches.

The full command:

$ telnet switchname.compname.com 

What I want to type:

$ enter 'switchname'

In turn, making the telnet command a simple enter and not having to consistently type .compname.com every time.

kvantour
  • 25,269
  • 4
  • 47
  • 72
  • 1
    Welcome to Stack Overflow. You require a function for this and not an alias. Have a look at [this](https://stackoverflow.com/questions/7131670) post. It tells you how you can achieve this (although for another example). – kvantour Jun 05 '19 at 15:59
  • Also take your time for the [tour]. – kvantour Jun 05 '19 at 16:00
  • 1
    Welcome to StackOverflow! Adding to what kvantour said already: A good first question, except for one thing: This site is not a programming service, i.e. people are expected to show and ask questions regarding their own effort to solve a problem. – Michael Jaros Jun 05 '19 at 16:07

3 Answers3

1

A simple function does nicely:

enter() { telnet $1.compname.com; }
ooga
  • 15,423
  • 2
  • 20
  • 21
  • Just to be clear, while this is a valid answer., it just gives a solution but it does not explain at all why a function should be used and how it works. Some references would be nice. – kvantour Jun 05 '19 at 16:02
  • `enter() { local swname=${1:-defaultswitch}; telnet $swname.compname.com; }` aliased to `alias mytelnet=enter` may prove more useful. – David C. Rankin Jun 05 '19 at 16:05
1

Create a function to open the telnet session to the switchname given by the first argument (or to your default switchname if no argument is provided), e.g. in .bashrc you could do:

mytelnet() {
    local swname=${1:-defaultname}  ## use local vars within function
    telnet $swname.compname.com     ## connect to your switch
}

Then create the alias you want for enter, e.g.

alias enter='mytelnet'

Now at the command line you can type:

$ enter                 ## to go to defaultname.compname.com

or

$ enter switchname      ## to go to switchname.compname.com

For testing you can just enter the function and alias on the command line, e.g.

$ mytelnet() { local swname=${1:-defaultname}; telnet $swname.compname.com; }
$ alias enter='mytelnet'

Then telnet away...

(note: you can simply name your function enter() and do away with the alias. I just find it convenient to define my functions at the top of my .bashrc and then create aliases, as needed, in the various sections below, but using an alias is by no means a requirement)

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 2
    Why create an alias? Just name the function `enter`. – chepner Jun 05 '19 at 16:46
  • No dying need, I generally just like the flexibility of using an alias to the functions at the top of my .bashrc. You could just name the function `enter()`, Habit more than anything since I use one .bashrc between multiple distros and have conditional blocks where aliases are assigned based on the `NAME` in `/etc/os-release`. – David C. Rankin Jun 05 '19 at 17:06
  • This worked perfectly! thank you so much! – Jonathan Ponce Jun 05 '19 at 18:36
  • Glad to help. Bash is an amazingly capable shell and there is a LOT to it. It's not something you learn in a day, a month, a year, ... So take your time and enjoy the learning process. There is no replacement for [`man bash`](http://man7.org/linux/man-pages/man1/bash.1.html) even if it is a little cryptic at first. It contains the whole story, it just takes a long while to digest. – David C. Rankin Jun 05 '19 at 18:55
  • Thanks a lot David, still very new at this so I will take your advice on taking it slow and even looking into man bash. – Jonathan Ponce Jun 05 '19 at 19:56
0

Try to use an alias in your rcfile, like ~/.bashrc, for example.

alias mytelnet='telnet the.desired.site'

Then source your rcfile like

source ~/.bashrc

or the equivalent

. ~/.bashrc

and type mytelnet to execute the command.

Or just use a bash variable, like

VAR="the.desired.site"

and execute telnet this way:

telnet $VAR

Also you can add a function to your rc file, like David C. Rankin mentioned.

function mytelnet () {
    telnet <<< "$@"
}
export -f mytelnet

<<< "$@" will feed telnet in the same way, as if you are using any other command on the commandline with their additional parameters, like in bash $1, $2 and so on. export -f marks the function mytelnet to be passed to child processes in your environment.

John Goofy
  • 1,330
  • 1
  • 10
  • 20
  • Perhaps have the alias be to a function that takes the switchname as an argument and then provide a `local` variable within the function set to the argument (or default switchname if none provided) could provide a bit more flexibility? – David C. Rankin Jun 05 '19 at 16:07
  • David, this have been a very good idea, please review my edit. – John Goofy Jun 06 '19 at 15:01
  • That's good improvement, though you ought to figure out how to make the `compname.com` a fixed part of the name so the user only has to provide the `switchname` part in order to connect. Also, since POSIX provides for the function taking arguments, you generally want to avoid bashisms (e.g. the *herestring* `<<<`) to provide the switchname when it can be done simply by passing as an argument (technically called a *positional parameter*). See if you can implement that with your solution. – David C. Rankin Jun 06 '19 at 17:46