capser
capser

Reputation: 2635

Bash %% parameter expansion cannot stat a file

I wrote up the script to first - take a line out of the foofile file. Then add a date to the tron_*.csv file and then gzip the file and then scp that file.

In order for me to add the date to the file I cut the suffix from the file - I cut off the csv from the file name and then append the date to the new file name and then reattach the "dot csv" to the file name and then I cp the $i file to the new name and then gzip and scp the file.

#!/bin/bash
set -x
sed -i '/D,642,0642,ZIPPY,FOO,,M,,S,S,FARFEGNUGEN,213,213,/d' ./tron_foot.csv
today=$(/bin/date +%Y%m%d)
#today=20141024
echo $today
for i in tron_foott.csv  tron_stk.csv
do
  cut_suffix=$(printf ${i%%.*})
  cp -p $i $cut_suffix.$today.csv
  gzip -f $cut_suffix.$today.csv
  sleep 2
  scp -r -p $cut_suffix.$today.csv.gz [email protected]:/data/DROPBOX/
  sleep 2
done

The problem is that the script no longer sees the files after I cut off the suffix.

I did not think it would work that way - the cp statement cannot see the original name of the file. I thought that cutting off the suffix of the file was just cosmetic. I did not think that it changed the value of $i - I thought that is just changed the name, not the contents of the file.

caper_user@casper_server:~$ /data/gprocess_cboe_tron_files
+ sed -i /D,642,0642,BEAR,TWIT,,M,,S,S,FARFEGNUGEN,213,213,/d /data/gtron_mmstk.csv
++ /bin/date +%Y%m%d
+ today=20141028
+ echo 20141028
20141028
+ for i in tron_mmet.csv tron_mmstk.csv
++ printf tron_mmet
+ cut_suffix=tron_mmet
+ cp -p tron_mmet.csv tron_mmet.20141028.csv
cp: cannot stat `tron_mmet.csv': No such file or directory
+ gzip -f tron_mmet.20141028.csv
gzip: tron_mmet.20141028.csv: No such file or directory
+ sleep 2
+ scp -r -p tron_mmet.20141028.csv.gz [email protected]:/data/DROPBOX/
tron_mmet.20141028.csv.gz: No such file or directory
+ sleep 2
+ for i in tron_mmet.csv tron_mmstk.csv
++ printf tron_mmstk
+ cut_suffix=tron_mmstk
+ cp -p tron_mmstk.csv tron_mmstk.20141028.csv
cp: cannot stat `tron_mmstk.csv': No such file or directory
+ gzip -f tron_mmstk.20141028.csv
gzip: tron_mmstk.20141028.csv: No such file or directory
+ sleep 2
+ scp -r -p tron_mmstk.20141028.csv.gz [email protected]:/data/DROPBOX/
tron_mmstk.20141028.csv.gz: No such file or directory
+ sleep 2
caper_user@casper_server:~$ cd /data/g

~

Upvotes: 1

Views: 135

Answers (3)

tripleee
tripleee

Reputation: 189638

The simple explanation is that the files do not exist in the first place.

I note that your code says ./ but your transcript says /data/g for the one operation which succeeded.

Perhaps you need to realize that relative paths are resolved from the current working directory of the process invoking the script. In other words, if you are in /tmp and run /home/you/script, the file name ./tron_mmstk.csv in the script resolves to /tmp/tron_mmstk.csv, not to /home/you/tron_mmstk.csv.

As an aside, you should properly double-quote all variables which contain file names, but that does not appear to be the problem here.

Furthermore, the printf is superfluous.

cut_suffix=${i%.csv}

Finally, the sleeps do not seem to serve any useful purpose.

Upvotes: 2

Etan Reisner
Etan Reisner

Reputation: 81002

Your paths are off. You use full paths in some places but (implicit) relative paths in others.

tron_mmet.csv does not exist in ~caper_user it exists in /data but the script is being run from ~caper_user and using a bare filename.

Upvotes: 2

Kevin
Kevin

Reputation: 30161

cp: cannot stat `tron_mmet.csv': No such file or directory

The value of $i has not changed. There is simply no file named tron_mmet.csv in the working directory. Is the script executing in the correct directory?

Upvotes: 1

Related Questions