ppeddi
ppeddi

Reputation: 13

unix unary operator error

I am trying to find a string in the input file path and replace it different file path if it matches.

Below is the code.

*

FilePath='/GAMER/WIP/Forecast'
echo $FilePath
fnd='WIP'
repl='Archive'
declare -i idx b
idx= expr index $FilePath 'Forecast'
echo $idx
if [ $idx -gt $b ]
then
   arch_dir=${FilePath/$fnd/$repl}
   echo $arch_dir

fi

*

echo statement properly displays the value as some number but in the if statement, I am getting unary operator error .

Can anyone please correct me where I am missing

Upvotes: 0

Views: 142

Answers (2)

ppeddi
ppeddi

Reputation: 13

thanks everyone who replied to my question...the issue was with index command and not keeping it in parenthesis. Syntax of [[ ]] is working but it gave new error of "missing Integer expression ". Below is the code which worked finally.

    fnd='WIP'
b=0
a=10
repl='Archive'
rep2=$(echo $FilePath | grep -c 'Forecast')

if [[ $rep2 -gt $b ]]
then
   arch_dir=${FilePath/$fnd/$repl}
  # echo $arch_dir
   dir=$FilePath
extn='.csv'
for file in "$dir"*FORECAST*.csv
do
  echo "$file"
  if [[ "$file" > $dir/null ]]; then
    fn=$(echo "$file"|cut -d "." -f1|rev|cut -d"/" -f1|rev)
  mv "$file" $arch_dir"$fn"_$dt$extn
  else
  echo "There are no FORECAST files in directory "$dir
fi
done
fi

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247240

It appears you have not defined the b variable, so when you write

if [ $idx -gt $b ]

the shell sees this after expanding the variables

if [ 12 -gt  ]

Use one of these forms:

if [ "$idx" -gt "$b" ]    # use quotes to maintain the operators as words
if [[ $idx -gt $b ]]      # the [[ form does not require quoting
if ((idx > b))            # use arithmetic expression

The first form will still throw an error if b is unset:

$ declare -i idx=12 b
$ [ "$idx" -gt "$b" ] && echo yes
bash: [: : integer expression expected
$ [[ $idx -gt $b ]] && echo yes
yes
$ ((idx > b)) && echo yes
yes

Upvotes: 3

Related Questions