简体   繁体   中英

Regarding Carriage Returns

I have created an sh script in Unix and it basically compiles a program specified via argument, for example: sh shellFile cprogram.c , and feeds the compiled program a variety of input files.

These input files are created in Unix and i have tested it, I am happy with those results. I have even tested it out with input files, made in Unix, that even had an extra carriage return at the end and yet i get good results, lets name this test file 00ecr for reference. Is it possible that if i created a test file in windows and transferred it over to Unix, that this new test file, lets call it 00wind , will produce bad results in my shell program.

This is just a theoretical question overall. I am just wondering if it will muck things up even though I tested my shell script with files, made in Unix, that accounted for extra carriage returns?

How about in your script, you use Linux command file to identify if the file has Windows style line terminations:

$file test.txt
test.txt: ASCII text, with CRLF line terminators

So your script could have a converting function like this:

#!/bin/bash

windows_file=`file $1 | grep CRLF`

if [[ -z "$windows_file" ]]; then
    echo "File already in unix format"
else
    # file need converting
    dos2unix $1
    echo "Converted a windows file"
fi

So here we first use the file utility to output the file type, and grep CRLF string, to see if the file needs converting. The grep will return null if it's not in windows format, and we can test for null string with if [[ -z "$var" ]] statement.

And then just dos2unix utility to convert it:

$ dos2unix test.txt
dos2unix: converting file test.txt to Unix format ...
$ file test.txt
test.txt: ASCII text

This way you could ensure that your input is always "sanitized".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM