0

I have bash shell and put the script in a file. It executes fine when I run it without sudo. When I run it as super user, it gives me error

[root@gums01 rotate_logs]# sudo ./rotate_logs.sh
./rotate_logs.sh: line 47: syntax error near unexpected token `<'
./rotate_logs.sh: line 47: `done < <(find  $LOGS_PATH/ -maxdepth 1 -mtime +$FILES_OLDER_THAN_DAYS -type f \( -name "gums-app.log_*.log" \) -print0)'

Here is the partial content of the shell script. I have tried many articles but not able to understand why this error pops up when running as super user. Please ignore the variables you see in below. Those are already defined in same file.

FILES_OLDER_THAN_DAYS=30    
files=()
    while IFS=  read -r -d $'\0'; do
        files+=("$REPLY")
    done < <(find  $LOGS_PATH/ -maxdepth 1 -mtime +$FILES_OLDER_THAN_DAYS -type f \( -name "gums-app.log_*.log" \) -print0)

    name=$(date --date="-$FILES_OLDER_THAN_DAYS day" '+%Y-%m-%d_%H:%M:%S')

    if (( ${#files[@]} )); then
      echo "[Time: $(date)] Making tar of GUMS log files older than $FILES_OLDER_THAN_DAYS days." >> $SCRIPT_LOG
      tar cfz "$LOGS_PATH/zippedLogs/backup_$name.tar.gz" "${files[@]}";
      echo "[Time: $(date)] Removing GUMS log files older than $FILES_OLDER_THAN_DAYS days from local" >> $SCRIPT_LOG
      find $LOGS_PATH/ -maxdepth 1 -mtime +$FILES_OLDER_THAN_DAYS -type f \( -name "*gums-app.log_*.log" \) -exec rm -f {} \;

      set_month_folder_id $LOG_GDRIVE_FOLDER_ID
      echo $target_folder_id >> $SCRIPT_LOG
      echo "[Time: $(date)] Uploading tar of log file to Google Drive" >> $SCRIPT_LOG
      $MY_PATH/gdrive upload $LOGS_PATH/zippedLogs/backup_$name.tar.gz -p $target_folder_id >> $SCRIPT_LOG
      echo "[Time: $(date)] Removing local tar file" >> $SCRIPT_LOG
      rm -rf $LOGS_PATH/zippedLogs/*;

    else
      echo "[Time: $(date)] No GUMS log found older than $FILES_OLDER_THAN_DAYS days" >> $SCRIPT_LOG
    fi
Waqar Ahmad
  • 3,716
  • 1
  • 16
  • 27
  • Do you have a shebang? Could it be that a different shell is used for the super user vs your user? – bgfvdu3w May 01 '19 at 01:24
  • Process substitution is bash-only (well, bash and other ksh-inspired extended shells) syntax; it isn't expected to work for `sh`. Having the `sh` tag on this question is thus a smell, as is not having a `#!/usr/bin/env bash` or similar shebang. – Charles Duffy May 01 '19 at 01:29
  • @Mark, ...moreover, with `sudo foo`, the interactive shell configured for root isn't invoked at all. Also related (showing what happens with an *invalid* shebang, equivalent to the behavior here where there's no shebang at all): https://unix.stackexchange.com/questions/250543/invoking-a-script-with-sudo-ignores-the-shebang – Charles Duffy May 01 '19 at 01:32
  • @WaqarAhmad, ...btw, I'd strongly suggest running your code through http://shellcheck.net/ and cleaning up all the warnings. When you're running code as root, you don't want bugs that can fall out from missing quotes -- I've literally seen a major data loss event caused by just that mistake (in a script intended to clean up old backups; a buffer overflow in a program generating filenames in the directory it operated over created a file with a whitespace-surrounded `*` in its name, and it unintentionally deleted *everything*). – Charles Duffy May 01 '19 at 01:34
  • ...events where those mistakes bite you may be rare, but when they do happen they can be very, *very* expensive. – Charles Duffy May 01 '19 at 01:35
  • I am using #!/bin/bash shebang. I did printenv and it use SHELL=/bin/bash – Waqar Ahmad May 01 '19 at 02:08

0 Answers0