How to concatenate this simple line in Unix?

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Infrastructure Monitoring How to concatenate this simple line in Unix?
# 1  
Old 06-29-2010
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!
# 2  
Old 06-29-2010
i have another way.

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

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

so you can simply execute command to execute the snmp querry
Code:
sn 227

This User Gave Thanks to amitranjansahu For This Post:
# 3  
Old 06-29-2010
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.
# 4  
Old 06-29-2010
No you are doing it wrong

create a file named sn
inside the file write
Code:
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
This User Gave Thanks to amitranjansahu For This Post:
# 5  
Old 06-29-2010
Quote:
Originally Posted by javygonx
Code:
alias sn='snmpget -c DreamTeam dal2-hr2'


Don't use aliases. Either use a script file or a shell function:
Code:
sn()
{
 snmpget -c DreamTeam "dal2-hr2.$1"
}

This User Gave Thanks to cfajohnson For This Post:
# 6  
Old 06-29-2010
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!
# 7  
Old 06-29-2010

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

Create a file containing all the functions.
This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with concatenate multiple line into one line

Hi, Do anybody experience how to concatenate multiple line into one line by using awk or perl command? Input file: >set1 QAWEQRQ@EWQEASED ASDAEQW QAWEQRQTQ ASRFQWRGWQ From the above Input file, it got 5 lines Desired output file: >set1... (6 Replies)
Discussion started by: perl_beginner
6 Replies

2. Shell Programming and Scripting

Concatenate small line with next line perl script

Hello to all, I'm new to perl, I have input file that contains the string below: 315350535ff450000014534130101ff4500ff45453779ff450ff45545f01ff45ff453245341ff4500000545000This string has as line separator "ff45". So, I want to print each line but the code below is not working. perl -pe '... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

3. Shell Programming and Scripting

awk concatenate every line of a file in a single line

I have several hundreds of tiny files which need to be concatenated into one single line and all those in a single file. Some files have several blank lines. Tried to use this script but failed on it. awk 'END { print r } r && !/^/ { print FILENAME, r; r = "" }{ r = r ? r $0 : $0 }' *.txt... (8 Replies)
Discussion started by: sdf
8 Replies

4. Shell Programming and Scripting

Logfile - extracting certain lines to concatenate into 1 line

I've got a log file from automatic diagnostic runs. The log file is appended to each time an automatic log is run. I'd like to just pull certain lines from each run in the log file, and concatenate them into 1 comma delimited line (for export into excel or an html table). Each diagnostic run... (3 Replies)
Discussion started by: BecTech
3 Replies

5. Shell Programming and Scripting

Concatenate strings line by line

Hi, I have a noob question . Can someone help me how to concatenate line by line using this variables? var1: Apple| Banana| var2: Red Yellow then how can I concatenate both line by line? in which the result would be: Apple|Red Banana|Yellow just to generate a row result i was... (6 Replies)
Discussion started by: hagdanan
6 Replies

6. UNIX for Advanced & Expert Users

need to concatenate two lines if the line doesnt end with quotes

Hi I am getting a source file where the columns are seperated by comma and double Quotes Eg1 : "AAA","BBB","CCCC" in the same source file i am also getting few lines where my last columns double quotes are ending in its next line or the next next line Eg2: "AAA","BBB","CCC CC"... (9 Replies)
Discussion started by: laxmi131
9 Replies

7. Shell Programming and Scripting

concatenate all duplicate line in a file.

Hi All, i have a zip file like the format 794051400123|COM|24|0|BD|R|99.98 794051413727|COM|11|0|BD|R|28.99 794051415622|COM|23|0|BD|R|28.99 883929004676|COM|0|0|BD|R|28.99 794051400123|MOM|62|0|BD|R|99.98 794051413727|MOM|4|0|BD|R|28.99 794051415622|MOM|80|0|BD|R|28.99 ... (30 Replies)
Discussion started by: vaskarbasak
30 Replies

8. Shell Programming and Scripting

concatenate and display 2 lines as 1 with a condition for 2 line ?

I have 2 pattern of lines (SQL query and Time taken)in a log i need to capture all SQL queries with time taken >20 sec and need to display as one line. 2 lines from log: 2007-10-23 11:39:17,061 DEBUG - SQL Query : SELECT A.GROUP_CD , C.FN_CD FROM UP_GROUP A , PRD_GROUP_TO_FN B , PRD_FN... (7 Replies)
Discussion started by: vithala
7 Replies

9. UNIX for Advanced & Expert Users

Need solution concatenate and display 2 lines as 1 with a condition for 2 line ?

I have 2 pattern of lines (SQL query and Time taken)in a log i need to capture all SQL queries with time taken >20 sec and need to display as one line. 2 lines from log: 2007-10-23 11:39:17,061 DEBUG - SQL Query : SELECT A.GROUP_CD , C.FN_CD FROM UP_GROUP A , PRD_GROUP_TO_FN B , PRD_FN... (1 Reply)
Discussion started by: vithala
1 Replies

10. UNIX for Dummies Questions & Answers

how to concatenate two command in one line and get the display in one screen

Hi, I would like to know , how to concatenate two command in one line and get the display in one screen for eg command 1 : ls -l /data/logs command 2 : ls -l /data/errors output shd be /data/logs /data/errors xx-xx-xx-xx abc.log xx-xx-xx-xx... (9 Replies)
Discussion started by: vasikaran
9 Replies
Login or Register to Ask a Question