Embedded Linux shell (BusyBox) check over list of files if they exist and run commands

StackOverflow https://stackoverflow.com/questions/23404720

  •  13-07-2023
  •  | 
  •  

質問

I'm trying to download a list of files (text file, one filename per line, no spaces or newlines in the filenames), and then check for each file if it exists and run commands accordingly. the first part seems to work just fine, the file from the web server is downloaded, and the first echo outputs the filenames. but the file existence check does not work.

#!/bin/sh
wget -qO- http://web.server/x/files.txt | while read file
do
        echo $file
        if [ -f $file ]; then
                echo $file exists
        else
                echo $file does not exist
        fi
done

output when executed in a directory where the second file (temp.txt) does exist:

file1.tmp
 does not exist
temp.txt
 does not exist
file3.tmp
 does not exist
file4.tmp
 does not exist

The second file does exist, and the echo commands in the if statement apparently doesn't recognize the $file variable either.

Any help is appreciated, I tried cobbling this together with info found here. A problem might be that this is not a full linux system, but embedded Linux (OpenELEC) with BusyBox v1.22.1.

UPDATE: thanks to the commenters we figured out that the code as is basically works fine AS LONG as the files.txt from the web server only contains unix EOL -- it doesn't work with windows CRLF line endings.

Now how could the script be made to work regardless of the line endings in the file from the web server?

役に立ちましたか?

解決

dos2unix is a utility which converts Windows line endings to Unix line endings. You can use it in your script like this:

wget -qO- http://web.server/x/files.txt | dos2unix | while read file

Or:

while read line; do
    ...
done < <(wget -qO- http://web.server/x/files.txt | dos2unix)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top