Echo working funny


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo working funny
# 1  
Old 06-23-2009
Echo working funny

Hello, this is my first post and question. I have search before for this problem but didn't find anything similar.

My case: I have a string inside the variable string1 like this:

string1="lala lele lili lolo lulu"

When I do echo of it, it appears like this:

echo
$string1
lala lele lili lolo lulu

But if I do echo with characters before and after, it appears like this:

echo "123 $string1 123"
123 lala
lele
lili
lolo
lulu 123


What's the problem?

NOTE: I have generated string1 with an AWK loop, but I have already test it with this example without AWK and the same happends!

Help me please!

Regards.
# 2  
Old 06-23-2009
Quote the variable:

Code:
echo "123 "$string1" 123"

Regards
# 3  
Old 06-23-2009
I will answer myself

It DOES NOT happend if you do it like in my example.

I think is something related with AWK. Is it inserting carriage returns between words? This is my AWK code.

It's runned in HP-UX. swList contains a list of packages and the comand swlist gives you the same list, but with 3 or more colums:
  • The first one the name of the pacjage binari.
  • The second one the version.
  • The third one and the rest the package full name.

Code:
for package in $swList; do

    # Package name
    package_name=$(swlist | grep "$package" | awk '{
    
        word_count=3
        
        while (word_count <= NF) {
            
            print $ word_count
            
             word_count++
            
        }
    }')
    
    # Package version
    package_version=$(swlist | grep "$package" | awk '{ print $2 }')
    
    echo "software^^^$oa_package_name^^^$oa_package_version^^^"

done

Help!

---------- Post updated at 04:49 AM ---------- Previous update was at 04:45 AM ----------

Quote:
Originally Posted by Franklin52
Quote the variable:

Code:
echo "123 "$string1" 123"

Regards

Thanks!

But it does not work with the string I made using that AWK code...

And there is a mistake in the code I put in my previous post. The last code line is:

Code:
echo "software^^^" "$package_name" "^^^" "$package_version" "^^^"

done

Just in case someone thought that was the problem Smilie

Regards

---------- Post updated at 05:04 AM ---------- Previous update was at 04:49 AM ----------

Well, I don't know why it happens but I have solved the problem doing the echo like this:

Code:
echo "software^^^"$(echo $oa_package_name | tr "\n" " ")"^^^$oa_package_version^^^ ^^^ ^^^ ^^^^^^ ^^^"

tr converts the carriage return into a space character.

Regards.
# 4  
Old 06-23-2009
This actually unquotes the variable!
Code:
echo "123 "$string1" 123"

Can you post the whole script and state you Operating System and Shell?
# 5  
Old 06-23-2009
Use printf in your awk code instead of a print statement, replace this:

Code:
print $word_count

with:

Code:
printf "%s " $word_count

# 6  
Old 06-23-2009
Quote:
Originally Posted by methyl
This actually unquotes the variable!
Code:
echo "123 "$string1" 123"

Can you post the whole script and state you Operating System and Shell?
hehe, yes! It does unquote the variable, because of that I put a pipe after the AWK loop like this:

Code:
        word_count++
            
        }
    }' | tr "\n" " ")

This worked fine!

HP-UX, KSH. The script is... too long to be posted! hehehe

Thank you very much anyway Smilie[COLOR="#738fbf"]

---------- Post updated at 05:41 AM ---------- Previous update was at 05:36 AM ----------

Quote:
Originally Posted by Franklin52
Use printf in your awk code instead of a print statement, replace this:

Code:
print $word_count

with:

Code:
printf "%s " $word_count


Didn't work, got this message in return:

Code:
awk: There are not enough parameters in printf statement %s sudo.
 The input line number is 1.
 The source line number is 7.

Trying to find the solution... this looks fancier than pipe the exit to tr statement Smilie

Regards

Last edited by hemtar; 06-23-2009 at 07:42 AM.. Reason: (quotation mistake)
# 7  
Old 06-23-2009
Not sure what you were trying to achieve here but beware that "swlist" on its own lists the bundles. Each bundle can contain multiple packages.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo command not working in the script

HI I have and echo command which works perfectly in the shell but when i execute in the script it gives me an error code query is as below QUERY=`echo "Select Severity,Dupl_count,Creation_Time,Last_Received,Node_Name,Node_Name,Object,Message_Group,Message_Text,Last_Annotation from " \ ... (2 Replies)
Discussion started by: Jcpratap
2 Replies

2. Shell Programming and Scripting

Echo not working with $

$cat FILE.txt $PATH1/file1.txt $PATH2/file2.txt where$PATH 1 = /root/FILE_DR/file1.txt $PATH 2 = /root/FILE_DR/file2.txt for I in `cat FILE.txt` do v=`echo $I` echo $v if then rm $v (5 Replies)
Discussion started by: ekharvi
5 Replies

3. Shell Programming and Scripting

echo two variables like the paste command is not working

Dear all, I have two files like this file1 A B C D E F file2 1,2 3,4 5,6 I want this output output_expected A B 1,2 C D 3,4 E F 5,6 (3 Replies)
Discussion started by: valente
3 Replies

4. Shell Programming and Scripting

echo is not working as expected

for i in `cat /export/home/afahmed/Arrvial_time.txt` do echo $i echo $i | awk '$3 < $D { print $4 }' >> dynamic_DF.txt; done When i echo, its echo as Nov 15 02:24 /export/home/pp_adm/inbound//wwallet_20111115.txt where i expect it to be Nov 15 02:24... (7 Replies)
Discussion started by: afahmed
7 Replies

5. What is on Your Mind?

Old, but still funny

Annoyances.org - Upgrading to Wife 1.0 (0 Replies)
Discussion started by: jgt
0 Replies

6. Shell Programming and Scripting

why the set rr='echo string|cut not working

I am new to the c shell script, can you let me know why the set rr= is not working. C shell script #! /bin/csh Set tt= 12345_UMR_BH452_3_2.txt set rr='echo $tt | cut –d”_” -f1' syntax error (4 Replies)
Discussion started by: jdsignature88
4 Replies

7. UNIX for Dummies Questions & Answers

Is echo $variable >> text.txt working in MacOSX?

Hi New at this, but want to learn more. I'm trying this as an Shell Command in MacOSX; newdate='<TIME>' echo $newdate >> /Users/ttadmin/Desktop/test.txt And it don't work. But if I just use; echo <TIME> >> /Users/ttadmin/Desktop/test.txt (<TIME> is an variable that one program... (6 Replies)
Discussion started by: jackt
6 Replies

8. UNIX for Dummies Questions & Answers

Funny but true....

Hallo everybody I am having a shell script called auto_run.sh in that only the first line works. the second line which has sed command is working only at the # prompt. not within the shell script. What could be the reason. *... sed 's/ //g' KTI >abc works in another shell script without the... (6 Replies)
Discussion started by: naushad
6 Replies
Login or Register to Ask a Question