1

An array holds the files accessed, and the archive files are split into smaller sizes in preparation for online backup. I am attempting to retrieve the exit code for each iteration through the loop of the split command. However, it is returning Exit Code 1, yet it says that the operation was successful. Why?

#!/bin/bash
declare -a SplitDirs
declare -a CFiles
CDIR=/mnt/Net_Pics/Working/Compressed/
SDIR=/mnt/Net_Pics/Working/Split/
Err=/mnt/Net_Pics/Working
SplitDirs=(`ls -l "$CDIR" --time-style="long-iso" | egrep '^d' | awk '{print $8}'`)
for dir in "${SplitDirs[@]}"
do
  if [ ! -d "$SDIR""$dir" ]; then
mkdir "$SDIR""$dir"
  else continue
  fi
CFiles=(`ls -l "$CDIR$dir" --time-style="long-iso" | awk '{print $8}'`)
  for f in "${CFiles[@]}"
  do
  if [ ! -e "$SDIR""$dir"/"$f" ]; then 
    split -d -a 4 -b 1992295 "$CDIR""$dir"/"$f" "$SDIR""$dir"/"$f" --verbose
    if [[ "$?" == 1 ]]
    then
      rm -rf "$SDIR""$dir" && echo "$SDIR""$dir" "Removed due to Error code" "$?""." "Testing Archives and Retrying..." 2>&1 | tee "$Err"/Split_Err.log
      7z t "$CDIR""$dir"/"$f" >> tee stdout.log 2>> "$Err"/"$dir"/7z_Err.log >&2
      mkdir "$SDIR""$dir" && split -d -a 4 -b 1992295 "$CDIR""$dir"/"$f" "$SDIR""$dir"/"$f" --verbose
       if [[ "$?" == 1 ]]
       then
          rm -rf "$SDIR""$dir" && echo "$SDIR""$dir" "Removed a second time due to Error code "$?". Skipping..." 2>&1 | tee "$Err"/Split_Err.log
          continue
       else
          echo "Split Success:" "$SDIR""$dir"/"$f" "ended with Exit status" "$?" && continue
       fi
    else
      echo "Split Success:" "$SDIR""$dir" "ended with Exit status" "$?" && continue
    fi 
  else
    echo "$SDIR""$dir"/"$f" "Exists... Skipping Operation" 2>&1 | tee "$Err"/"$dir"/Split_Err.log
    continue
  fi
  done

(The echo piping in a previous revision of the question was misplaced code, and thank you for pointing that out. The exit code remains the same, though. Overall,the script does what I want it to except for the exit code portion.)

1
  • 2
    That's some serious coding style inconsistency. There are quite a few things wrong with this script that could cause it to break, like the fact that you're piping through your echo $?, you aren't quoting your variables, you're redirecting (instead of piping) through tee... Perhaps you could try cleaning up the script first? Commented May 26, 2012 at 5:19

1 Answer 1

1

Remove | echo $?. you are processing the return code of echo command(last command).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.