2

I am trying to run the following as part of a application start script:

for file in $TEMPLATE_FOLDER/*
do
    filename='basename $file'
    echo "Processing $filename"

    if [ -f "$CONF_FOLDER/$filename" ]
    then
        echo "Using existing $filename"
    else
        echo "Creating $filename from template $file"
        cp $file $CONF_FOLDER
    fi
done

But the output is:

Processing basename $file
Creating basename $file from template ../conf/templates/conf-TEST/log4j.xml

The basename command is not evaluated and is just printed out.

When i run the command from the prompt it works fine.

Bash version:

bash -version
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2004 Free Software Foundation, Inc.

Can anyone see what i am doing wrong?

Thanks!

KasperF
  • 332
  • 1
  • 4
  • 14
  • [Get basename of filename or directory name](https://www.cyberciti.biz/faq/bash-get-basename-of-filename-or-directory-name/) – jww Apr 07 '19 at 18:50

1 Answers1

9

Since the system has bash, and the script works with bash, first change line #1 of the script from #!/bin/sh to #!/bin/bash.

Then change this line:

filename='basename $file'

To this:

filename=`basename $file`
agc
  • 7,973
  • 2
  • 29
  • 50
  • @monokrome, True, but the OP's system *does* have `basename`. For a pure `bash` method please try `filename="${file//+(*\/)}"` and refer to [Extract file basename without path and extension in bash](https://stackoverflow.com/questions/2664740/extract-file-basename-without-path-and-extension-in-bash/38277789#38277789). – agc Mar 21 '20 at 08:07