简体   繁体   中英

bash script cd command affecting tee command

I've a script to perform git operations.

  • Parse the file for the repo, source and new branch
  • Clone the repo
  • check if the new branch already exists. skip this repo if it does
  • create new branch:
  • cd into the folder
    • git branch new_branch
    • git checkout new_branch origin/source_branch
    • git push origin new_branch
    • print the status piping with tee (stdout and file)
  • reset: cd out of the folder and delete the folder.
  • next entry

The print to the file works only if I remove the 'cd' command. It print fine onto the stdout but not the file. Any idea why is 'cd' affecting the 'tee'?

input.csv: repo,branch1,branch2, git@github.com/myproject/test-1.git, feature1, feature2, git@github.com/myproject/test-2.git, feature1, feature2,

Simplified script:

#!/bin/bash

BASE_DIR=`pwd`
INPUT_FILE_CSV=input.csv
OUTFILE="output.txt"
[ -e $OUTFILE ] && rm -f $OUTFILE

while IFS="," read -r REPO SRC_BRANCH NEW_BRANCH
do
  echo "**********************Repo: ${REPO} **********************"
  REPO_NAME=`echo $REPO | rev |cut -d '/' -f1 |rev | sed 's|.git||g'`
  rm -rf $REPO_NAME
  ERR=$(git clone -q $REPO)
  if [ $? -eq 0 ]; then
    #Logic to check if the branch exists
#      cd $REPO_NAME
      # Run few other git commands
      echo "${REPO_NAME}; Success;" | tee -a $OUTFILE
    cd $BASE_DIR
    rm -rf $REPO_NAME
  else
    echo "${REPO_NAME}; Failure; ERR: ${ERR}" | tee -a $OUTFILE
  fi
done < <(cut -d "," -f1,2,3 $INPUT_FILE_CSV | tail -n +2)

Expected output:

test1;Success;
test2;Failure;Err: $ERR

Current output:

test-1; Failure; ERR: 

NOTE: The git URL in the input.csv aren't real/valid.

tee -a $OUTFILE expands to tee -a output.txt , which writes to a file named output.txt in the current working directory . Change the current working directory, and you change where that file get written. In this case, it's creating the output.txt file inside the $REPO_NAME directory, which gets deleted just a few lines later.

You could fix this by using an absolute path for OUTFILE , but if your OS supports /dev/fd/ entries to access file descriptors by number I'd be tempted to use that instead:

while IFS="," read -r REPO SRC_BRANCH NEW_BRANCH
    ...
    echo "${REPO_NAME}; Success;" | tee /dev/fd/3    # Copy to file descriptor #3
    ...
done < <(cut -d "," -f1,2,3 $INPUT_FILE_CSV | tail -n +2) 3> "$OUTPUT"
#                          this sends FD #3 to output.txt ^^^^^^^^^^^^

This way, the output file is opened outside of the loop, before any directory changing has happened (and once it's open, changing the working directory doesn't affect it).

BTW, you should run your script through shellcheck.net ; it'll have a number of good recommendations for improving your script. Checking for errors on cd commands is particularly important, since if cd fails it'll run the following commands in the wrong place, which can have very bad consequences .

Additionally, I'd recommend switching to lower- or mixed-case variable names, since there are many all-caps names with special meaning/function, and accidentally using one of those for something else can cause problems.

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