0

In a follow-up question to a question asked yesterday:

I am running the below command in Datastage ETL tool, which executes the script in Linux.

I executed:

for file in /Path/filename_*; do [[ $(wc -l "$file" | cut -d' ' -f1) -eq 1 ]] && rm $file ; done
Reply=0

In this case I'm having empty files in the folder:

for file in /Path/filename_*; do [[ $(wc -l "$file" | cut -d' ' -f1) -eq 1 ]] && rm $file ; done
Reply=1

I don't have empty files in the folder.

Both are same commands with different reply, my job fails if i get reply code 1

  • 3
    Your command seems incomplete: do is a reserved word to be used in a for loop, which is missing in the command you show. Are you sure this is the complete extract from your shell script? If so, I recommend using shellcheck to debug your script. – AdminBee Jun 26 '20 at 13:59
  • sorry, i have updated the command now and updated the question – Aditya Kutcharlapati Jun 26 '20 at 14:52
  • So, your second command actually also starts with for file in? If so, please edit your post to include that. Also, you don't mention the syntax error any more, and it is unclear now what the problem really is (what does it mean if you say that the command is not successful any more). Please edit to clarify. – AdminBee Jun 26 '20 at 14:54
  • both are same commands, the issue is with the Reply. When i get reply 1, the job fails. The job should run successfully even if there are no empty files – Aditya Kutcharlapati Jun 26 '20 at 15:21
  • Same problem as in your other question (last command run in the loop has a non-zero exit status), same solution(s). See my answer there. – Stéphane Chazelas Jun 26 '20 at 15:40
  • https://stackoverflow.com/q/62592957 – alecxs Jun 26 '20 at 17:22
  • I have read 3 times, but see no question. What is the question. The only bit that I can workout, is that you have "empty files in the folder" and you "don''t have empty files in the folder". You need to open the box and examine the cat. – ctrl-alt-delor Jun 26 '20 at 17:32

1 Answers1

2

To avoid leaving a non-zero status from [[, you could use an if statement rather than && operator:

if [[ $(wc -l <"$file") -eq 1 ]]; then
  echo rm -- "$file"
fi

Instead of counting all lines, you may only need to read up to the second line:

for file in /Path/filename_*; do
  [ -f "$file" ] || continue
  {
    read _
    read _ || echo rm -- "$file"
  } <"$file"
done

If the second read fails, then there is no second line (according to POSIX).

guest
  • 2,134