How to concatenate this simple line in Unix?

Hi:
I use the snmpget command everyday. And Im getting tire of writing the same line evertime I have to verify something.

Example of the line:

snmpget -c DreamTeam dal2-hr2 ifAlias.227

The .227 its the circuit interface and also its variable; could be any other number depending on the circuit id. So I create an alias in which it works:
alias sn='snmpget -c DreamTeam dal2-hr2'

So, now all I have to do its to write "sn ifAlias.227" and works fine; but!
if I create a new alias like:

alias sn='snmpget -c DreamTeam dal2-hr2 ifAlias." makes me an error;
so when I write "sn 227" it fails. Also if I did the alias without the "." and run the "sn .227" also fails.

How can I add the .227 to that ifAlias string. Or how to create a variable so
the "ifAlias.xxx" so makes the command works?!!

Thanks in advance!

i have another way.

create a script and put it in your path with execute permission

#cat sn
snmpget -c DreamTeam dal2-hr2 ifAlias.$1

so you can simply execute command to execute the snmp querry

sn 227
1 Like

Hi again:
Thanks for the info. But Im doing something's wrong that its not working.
I create a "test.env" file. Did the chmod 755 test.env

Inside the file I wrote:
# cat sn
snmpget -c DreamTeam dal2-hr1 ifAlias.$1

I save the file; but something should be wrong on the script that doesnt work. Possible its my problem doing something that is missing.

I have a script named "snmp.env" with few snmpget commands that works.
So, maybe Im missing something on the script code or with the cat. Please help.

No you are doing it wrong

create a file named sn
inside the file write

snmpget -c DreamTeam dal2-hr1 ifAlias.$1

give execute permission
chmod 755 sn

make sure that the file is created in your PATH.
To check your path
echo $PATH

then you can use
sn 226

1 Like

Don't use aliases. Either use a script file or a shell function:

sn()
{
 snmpget -c DreamTeam "dal2-hr2.$1"
}
1 Like

Wow!.. Interesting. I left at work but I will try tomorrow!. Thanks a lot.
By the way, you said about a shell script. Let me explain something to see if I can make a better program than the aliases I created.

I have created an executable file named "snmp.env". Inside the file I have the following:

alias dal1='snmpget -c DreamTeam dal2-hr1'
alias dal2='snmpget -c DreamTeam dal2-hr2'
alias lon1='snmpget -c DreamTeam lon1-hr1'
alias lon2='snmpget -c DreamTeam lon2-hr2'

And so on... Its like 12 aliases like that. So when I ran it I just do:
dal1 ifAlias.236 [ENTER], and so on. So as you explained me and I told you the .236 its a variable; it could be any number.

I understood of creating the 'sn' file and inside it to add in the file:
snmpget -c DreamTeam ifAlias.$1 so all I have to do to run its
its just "sn 236" and thats great!...

Now about the shell function; How can I add all other aliases to the shell function? Or do I have to create each shell function individually instead of creating a nice shell function with all aliases I want?

Thanks again!

I repeat: Don't use aliases; use functions.

Create a file containing all the functions.

1 Like

Thanks for everything. I was able to create every single file for each snmp I have to run. Twelve files; but works great! All I have to do now its doing the "dal1 227" and done!...

What I was trying to do the same thing but in a shell function; but couldnt.
No idea how to do it. I create a file for test named "sn.sh" and inside the file
I wrote the following code:

sn()
{
snmpget -c DreamTeam dal2-hr1 ifAlias.$1
}

But I have no idea how to call it and run it or if there's something wrong or missing. Can anyone help? Thanks!!

To load the functions, source the file with:

. sn.sh

Ok, and where did I put the variable $1. This variable its a numeric name; Could be any number between 1-1000?

I tried:

. sn.sh 227 -> No output is given
. sn 227.sh -> Error

Load the functions then call them:

. sn.sh
sn 227

I got it. Thanks. I did the function and for some reason when I ran it it doesnt like it. It says "Too Many Arguments".... But when I did the script named "sn" and inside the file I wrote "snmpget -c DreamTeam ifAlias.$1". All I have to do to run its is "sn 227" and works.

But on the function it doesnt.

How is the function defined? What is in it?

How did you call the script? the function?

Please post the exact code you used.

Ok. Name of the function file "sn.sh"

Code in the file "sn.sh":
##################
sn()
{
snmpget -c DreamTeam dal2-hr1 ifAlias.$1
}
##################

To call function: . sn.sh [ENTER]

To run it: "sn 227" [ENTER]