Jeff_H
Jeff_H

Reputation: 9

UNIX Shell Set a Variable in a UNIX Shell Script to a Directory Path Plus File Name

I am trying to set a variable in a UNIX Shell script to a directory path plus file name with a date stamp. At the command prompt this produces the results. When I echo FILE_DATE back to the screen in the script is produces The name $FILE_DATE not the result. I thought this would be easy. I am apparently missing something easy.

My code:

FILE_DATE=echo ls -1 /opt/ftp/receive/SSA_SSI/SSA_Accounts_*.csv | grep -oP '[\d]+[\d]+[\d]+'

echo $FILE_DATE 

Upvotes: 0

Views: 153

Answers (1)

milanbalazs
milanbalazs

Reputation: 5339

  1. The parentheses are missing around your command.
  2. The $ character also missing.
  3. The echo command is not needed.

Working script:

FILE_DATE=$(ls -1 /opt/ftp/receive/SSA_SSI/SSA_Accounts_*.csv | grep -oP '[\d]+[\d]+[\d]+')

echo ${FILE_DATE}

I have created a little presentation how it works:

>>> ls
7093966790966902785_n.jpg  first_test.py  second_test.py  test.py
>>> MY_DATA=$(ls -1 *.py | grep "second")
>>> echo ${MY_DATA}
second_test.py

Upvotes: 1

Related Questions