0

I'm trying to create a script for running two different commands according to the names of the files in a directory.

So far I created this small script:

#!/bin/bash

declare -a sumas=("CPRAT1sfc.nc" "CPRAT2sfc.nc" "CPRATsfc.nc")

for files in *.nc; do
    if [ "$files"="${sumas[@]}" ]; then
        cdo daymean $files {$files}_d.nc
    else
        cdo daysum $files {$files}_d.nc
    fi
done

I'm trying to get all the files that end in .nc, then check if they are named different to the names in the sumas array.

For the files that are named the same, a command will run: cdo daymean input output and the output file will have the same name as the input but with a _d.nc at the end (still have to remove the .nc of the input so they won't end up as .nc_d.nc).

If their names are different to sumas then another command will run, with a similar syntax as the previous.

I though the script was ready but I'm getting this error: scriptCDO.sh: 4: scriptCDO.sh: Syntax error: "(" unexpected

Any idea of what is happening?

Guillermo.D
  • 399
  • 2
  • 14
  • The immediate problem is that you're trying to run a bash script with an interpreter for a different language, specifically `sh`. Use `bash` instead, or preferably, `./yourfile` to use the file specified in the shebang. However, there are several other problems. Use shellcheck and fixed those issues first, then update. – that other guy Jun 05 '18 at 23:42
  • can you put `echo BASH_VERSION=$BASH_VERSION` inside this script, run it and show it's output? – KamilCuk Jun 05 '18 at 23:55
  • @KamilCuk it shows BASH_VERSION=4.4.12(1)-release – Guillermo.D Jun 06 '18 at 03:42
  • Thanks @thatotherguy !. I'm kind of new on this... after your comment I was able to run it with ./scriptname but now I get that a binary operator is expected on the "if..." line. :/ I thought it was currently written. – Guillermo.D Jun 06 '18 at 03:42

1 Answers1

1

Don't have enough information because $i is not set but your issue had to do with the comparison method mainly. Have a look at this :

#!/usr/bin/env bash
BASE_DIR="/tmp/"  # change this to your base folder name
declare -a sumas=( "CPRAT1sfc.nc" "CPRAT2sfc.nc" "CPRATsfc.nc" )
i=1 # you need to set this to something

cd "${BASE_DIR}"
for each in * ; do
    if [[ "${sumas[@]}" =~ "${each}" ]] ;then
        echo "HERE -- cdo daymean $each {$i}_d.nc"
    else
        echo "NOT HERE -- cdo daysum $echo {$i}_d.nc"
    fi
done
Mike Q
  • 6,716
  • 5
  • 55
  • 62
  • Thanks for your observation! I already update it but now I get that a binary operator is expected on the "if..." line – Guillermo.D Jun 06 '18 at 03:45
  • @Guillermo.D , sorry before I did not test code, the above code works for me on BASH4+. – Mike Q Jun 06 '18 at 04:08
  • Thanks! But one easy question... why is that in this case we need the [[ instead of a single [ ? – Guillermo.D Jun 06 '18 at 04:44
  • good question, there are many ways to test but the one we are using '=~' would read incorrectly with single quotes, I use phpStorm and with bash extension and it tells me there is a problem when I remove one of the [ – Mike Q Jun 06 '18 at 04:54
  • have look at this post (and mine in this post) https://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash/50572594#50572594 – Mike Q Jun 07 '18 at 15:05