About reture with string and keep other "echo" info


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting About reture with string and keep other "echo" info
# 1  
Old 12-17-2012
About reture with string and keep other "echo" info

I use below code to get ip, and I want to echo something in the function, but the $IP will be all the value from "echo" I just want to get the real ip, and keep the echo info, any help

Code:
#!/bin/bash
get_ip()
{
    ip=$(grep -B2 "00:01:02:03:04:05" ip_tmp.log |head -1 |awk '{print $2}')
    echo "............."
    echo "Guest OS ip is $ip" 
    echo "............."
     #rm ip_tmp.log
    echo $ip
}
IP=$(get_ip)
echo "IP is $IP"


Last edited by yanglei_fage; 12-17-2012 at 08:56 AM..
# 2  
Old 12-17-2012
If the (lowercase) variable ip is set, you could just echo that.
# 3  
Old 12-17-2012
Quote:
Originally Posted by Scott
If the (lowercase) variable ip is set, you could just echo that.
I may cause some misunderstanding I updated my question ip()--> get_ip()
# 4  
Old 12-17-2012
That was clear, but doesn't change anything.

The variable ip which is set in the function is available after the function is called. You can use that to echo the IP address, without affecting anything else.

If you really want to use the value of $IP and keep the other stuff, send the other stuff to standard error.

Code:
#!/bin/bash
get_ip()
{
    ip=$(grep -B2 "00:01:02:03:04:05" ip_tmp.log |head -1 |awk '{print $2}')
    echo "............." >&2
    echo "Guest OS ip is $ip" >&2
    echo "............." >&2
     #rm ip_tmp.log
    echo $ip
}
IP=$(get_ip)
echo "IP is $IP"

# 5  
Old 12-17-2012
Quote:
Originally Posted by Scott
That was clear, but doesn't change anything.

The variable ip which is set in the function is available after the function is called. You can use that to echo the IP address, without affecting anything else.

If you really want to use the value of $IP and keep the other stuff, send the other stuff to standard error.

Code:
#!/bin/bash
get_ip()
{
    ip=$(grep -B2 "00:01:02:03:04:05" ip_tmp.log |head -1 |awk '{print $2}')
    echo "............." >&2
    echo "Guest OS ip is $ip" >&2
    echo "............." >&2
     #rm ip_tmp.log
    echo $ip
}
IP=$(get_ip)
echo "IP is $IP"


Thanks it works

Lei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

3. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

4. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. AIX

echo $varibla | mail -s "subject" "xxx@xxx.com" not ruuning as expected

Hi Folks, As per the subject, the following command is not working as expected. echo $variable | mail -s "subject" "xxx@xxx.com" Could anyone figure it out whats wrong with this. I am using AIX box. Regards, (2 Replies)
Discussion started by: gjarms
2 Replies

7. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

8. HP-UX

echo "selall;info;wait;infolog" | /usr/sbin/cstm problem

Hello, On a HP-UX 10.20 server I've executed something similar to this command: # echo 'selall;info;wait;infolog;view;done' | /usr/sbin/cstm But it returns sometype of "argument list too long" error. I suppose there is a way to fix it by using xargs but I can't figure it out. Any... (7 Replies)
Discussion started by: asanchez
7 Replies

9. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

10. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies
Login or Register to Ask a Question