0

I am unable to copy files using Shell script. I validated in shellcheck.net no issues found in syntax.

It showing error as file not fount but I am using exact file name.

Below is my Shell script.

#! /bin/bash
export SOURCE_PATH="/home/la1122/export"
export SOURCE_TARGET="/home/la1122/export/backup"
FILE="*.dat"
cd $SOURCE_PATH
echo "Current location: $(pwd)"
cp "$FILE" "$SOURCE_TARGET"
Reach Loki
  • 31
  • 4
  • When a question is closed, you can still [edit] it, and should do so rather than asking a new one. – Charles Duffy Jun 22 '22 at 21:23
  • That said -- what exactly is the error message this time? (Last time the error you had was caused by your script being saved with DOS newlines). – Charles Duffy Jun 22 '22 at 21:24
  • ..._oh_. `"$FILE"`. That doesn't work, because the glob isn't expanded. (It's a new/different problem, because you weren't quoting `$FILE` in the last question). – Charles Duffy Jun 22 '22 at 21:24
  • Use `files=( *.dat )`, and then `cp "${files[@]}" "$SOURCE_TARGET"` – Charles Duffy Jun 22 '22 at 21:24
  • ...mind, you shouldn't be using all-caps names for your own variables at all (and you also shouldn't be `export`ing variables without a good reason to do so). – Charles Duffy Jun 22 '22 at 21:25
  • Also, use `$PWD` instead of `$(pwd)` -- the latter is much slower, because it makes a whole new copy of your shell to run the `pwd` command in. – Charles Duffy Jun 22 '22 at 21:25
  • Thank you very much sir. I am new to this Shell script so pls excuse me for dumb mistakes. I did tried as you said. #! /bin/bash export SOURCE_PATH="/home/la1122/export" export SOURCE_TARGET="/home/la1122/export/backup" file=(*.dat) echo "Locatio: $PWD" cp "${file[@]}" "$SOURCE_TARGET – Reach Loki Jun 22 '22 at 21:46
  • I am still getting error says cp: cannot stat a(*.dat)\ra: No such file or directory – Reach Loki Jun 22 '22 at 21:47
  • Go ahead and [edit] the question with the new code and the error (it's frowned on to do that if an answer was added with "Add an Answer" and an edit would make it incorrect, but these are all just comments so you don't need to worry about that) – Charles Duffy Jun 22 '22 at 21:47
  • BTW, maybe you want `file=( "$SOURCE_PATH"/*.dat )` instead of just `file=( *.dat )`? – Charles Duffy Jun 22 '22 at 21:48
  • Okay, the `\r` says you still didn't fix the problem with DOS newlines. The old question was closed with a link to a duplicate describing how to solve that. – Charles Duffy Jun 22 '22 at 21:49
  • You can use a tool like `dos2unix`, or you can configure whatever editor you used to write the code to save your file in UNIX format. – Charles Duffy Jun 22 '22 at 21:50
  • I am writing the script in Unix itself – Reach Loki Jun 22 '22 at 21:55
  • Like vi test.sh – Reach Loki Jun 22 '22 at 21:55
  • Run `bash -x test.sh` to log execution, and [edit] the log into the question. – Charles Duffy Jun 22 '22 at 22:14
  • Thank you very much sir for your patience. I figured it out. I didn’t find answer tab to post what resolved my issue. – Reach Loki Jun 22 '22 at 22:18
  • I created new text document in notepad++ before I write any code in the edit tab went to EOL Conversation and selected as UNIX. Then I started coding. file=(*.dat) and cp “${file[@]}” “$SOURCE_TARGET”. This two command fixed my issue. – Reach Loki Jun 22 '22 at 22:25
  • BTW, in vim, `:set fileformat=unix` will make it save files with LFs even if before that file used CRLFs. – Charles Duffy Jun 22 '22 at 22:28
  • BTW why not just `cp "$SOURCE_PATH/"*.dat "$SOURCE_TARGET"` (no need for `cd`, nor `FILE`) – user16320675 Jun 22 '22 at 22:43
  • Yes, will update it accordingly. Thank you. I just had for clear understanding. – Reach Loki Jun 22 '22 at 22:47
  • @CharlesDuffy Sir I don’t know vim. I am using $vi filename to edit the file – Reach Loki Jun 22 '22 at 22:51
  • `vim` is "vi improved"; it's a newer alternative to vi with more features, but otherwise works pretty much the same way. – Charles Duffy Jun 22 '22 at 23:42

0 Answers0