shell script output problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script output problem
# 1  
Old 07-13-2012
shell script output problem

Hello, I am writing a script in ksh to output a data file that is pipe delimited (not my choice). In any case, it has me scratching my head since the output is not as expected.
Code:
exec 3<acc.txt   # contains 3 digit number, 1 per line
exec 4<ssid.txt  # contains 8 digit numbers, 1 per line

set -A acc_f "Y|Y|Y|Y" "N|N|N|N" "Y|N|Y|N" "N|Y|N|Y"
set -A stype L S R N

while read -u3 acc; do
  for i in 0 1 2 3; do
      read -u4 ssid
      echo "$ssid|2012|$acc|${acc_f[$i]}|${stype[$i]}"
  done
done

The output ignores "$ssid|2012|$acc" for some reason. I can print them separately if I include some echoes before that line but that line only prints the right half starting from "|${acc_f[$i]}...". Does anyone have any ideas what is going on?

Thanks,
Ben

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 07-13-2012 at 11:56 AM..
# 2  
Old 07-13-2012
First off, it would have been nice to tell the interested audience which shell you are using. I take it, from the "read -u" command, which is not there in bash, that you are using a ksh (probably ksh93), but instead of letting me hone my investigative talents it is generally more fruitful to describe your environment (this includes the OS you are on - crystal balls are in short supply nowadays).

And while i am at it: giving the thread a meaningful title would also have helped. Something like "display variables after reading from file" would probably have gotten more answers in shorter time.

Quote:
Originally Posted by benalt
Code:
echo "$ssid|2012|$acc|${acc_f[$i]}|${stype[$i]}"

You should probably use "print" instead of "echo" - "print" is a builtin and offers more possibilities. But this only as an aside.

I suppose it is because "|" is a special character to the shell. Try the following, which is a bit safer:

Code:
print - "${ssid}|2012|${acc}|${acc_f[$i]}|${stype[$i]}"

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 07-13-2012
Firstly, thanks for the response. While I did mention I was using ksh I should have mentioned that I am using both ksh93 and cygwin. I am assuming there is something not right with this combination. When I substituted your print line, the output I got inexplicably had the fields in the wrong order: |N|Y|N|Y|N|2012|901

I.e. it wrote the right half of the print first and skipped the ${ssid} completely before printing the left side. I just rewrote it in awk in my frustration and it worked fine.

In any case, I appreciate your help and scripting advice.
Thanks,
Ben
# 4  
Old 07-15-2012
As a quick update to this problem. I ran my ksh93 script at home on CentOS and had the same puzzling results. I did a od -cx on the files after a quick look at the results, what stood out was the \r\n in the files. I did a dos2unix on the files and the script now works. I presume this is the result of working with Windows based cygwin. The results were still quite unexpected. I thought I might update this info here as it I guess pays to check what is really contained in your data files.
This User Gave Thanks to benalt For This Post:
# 5  
Old 07-15-2012
Thanks to the carriage returns, it only looked like it wasn't being printed. When the shell read the lines into the variables, using the \n as the delimiter, the string assigned to the variables ended in \r. So, when you were echoing them, the embedded \r in the string was causing the teriminal's cursor to return to column1 and overwrite what had been there.

Once you've been bitten by this, you tend to remember it.

Thank you for reporting back with your findings. At some point in the future, someone may find it useful.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 6  
Old 07-15-2012
Thank you for posting the solution. It is a common problem which appears daily on unix.com. Mainstream unix uses line-feed as a record terminator in text files whereas MSDOS (and thence Microsoft Windows) uses the two characters carriage-return line-feed.

This is a long-standing design issue with Microsoft Operating Systems. It does mean that in early MSDOS you could output a text file to a printer port (with no proper printer driver) and it would print ok.

If you want to quickly check a file, this command will make line terminators visible :
Code:
sed -n l filename.txt


Last edited by methyl; 07-15-2012 at 08:39 PM.. Reason: addenda
This User Gave Thanks to methyl For This Post:
# 7  
Old 07-16-2012
Quote:
Originally Posted by methyl
If you want to quickly check a file, this command will make line terminators visible :
Code:
sed -n l filename.txt

Absolutely right.

Another way is in vi to enter ":set list" in command-mode. This makes all non-printable characters (line feeds, tabs, etc.) visible. Switch off with ":set nolist".

Thanks go to the threads O/P for writing a follow-up and bothering to share his solution. Thanks also to alister for the explanation.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to call and sort awk script and output

I'm trying to create a shell script that takes a awk script that I wrote and a filename as an argument. I was able to get that done but I'm having trouble figuring out how to keep the header of the output at the top but sort the rest of the rows alphabetically. This is what I have now but it is... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

3. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

4. Shell Programming and Scripting

Customize the Shell Script output

Hello guys. i wan to write an script to Customize this output: -------------------+--------------------+---------+--------- ias-component | process-type | pid | status -------------------+--------------------+---------+--------- DSA | DSA | ... (10 Replies)
Discussion started by: Ymir
10 Replies

5. Shell Programming and Scripting

Problem in formatting output of SQL query in excel sheet in shell script

Hi Guys.. Need your help to format the output of my shell script. I am using spool command to take out put in csv file. below is my code. (for example) col USERNAME for a15 col EMAIL for a30 col FULL_NAME for a20 col LAST_LOGIN for a40 col DATE_CREATED for a40 SPOOL 120.csv... (3 Replies)
Discussion started by: Agupte
3 Replies

6. Shell Programming and Scripting

Shell Script output

Hi All, Below is the shell script for which desired output is required: Shell script: #!/bin/bash . /oracle/TEST/db/tech_st/11.1.0/TEST_<hostname>.env sqlplus /nolog << EOF connect / as sysdba spool /home/oracle/db_output.log @lockwait.sql prompt Database Locks ... (1 Reply)
Discussion started by: a1_win
1 Replies

7. Shell Programming and Scripting

shell script no output

xxxxxx (4 Replies)
Discussion started by: vinayrao
4 Replies

8. Shell Programming and Scripting

Problem capturing output in TCL script

I have a TCL script that logs into a switch using expect.I send a command "show port-security address" and it returns a table having a large number of rows.I need to capture this output(the table) and store it in a .txt file. I have done this: match_max 5000 set expect_out(buffer) {} set... (0 Replies)
Discussion started by: plasmalightwave
0 Replies

9. Shell Programming and Scripting

[How To?] Run shell script and get output into another shell.

Hi guys, I have a simple question, I want to store the output of the following command: As you can see it is running all the time, and we get a new line every 3sec. I just want to store these new lines into a single variable, so I can use it into a script. To clear the screen, and... (4 Replies)
Discussion started by: Thireus
4 Replies

10. Shell Programming and Scripting

Shell Script Output

I'm trying to automate numerous grep searches via shell scripting. However, the output of echo is ONE line whereas grepping from the command line would give line breaks as seen in the file. For example: test.txt: 12345 - hi 12345 - how 54321 - are 12345 - you grep output would be:... (4 Replies)
Discussion started by: sharpi03
4 Replies
Login or Register to Ask a Question