-1
#!/bin/bash
mysql -uroot -pwelcome1 < /home/sai/first.sql

echo "The program has completed"

I am trying to call a sql file from shell script ,I am running the script as bash run.sh, but it telling that there is no such directoryfirst.sql, but if i run that command separately as mysql -uroot -pwelcome1 < /home/sai/first.sql, it is working, is there a different way to write?

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89

1 Answers1

2

This is probably a line-ending issue. If you open run.sh in vim, and run set fileformat=unix and the + appears (to show you that the file is now modified) it means that the file had DOS line endings, which Bash cannot interpret correctly. Save and quit and the script should work.

It future, if something obviously weird is going on, please use a command like od -ctx1 run.sh to look at what is really in the file.

Alastair Irvine
  • 1,182
  • 11
  • 24
  • I agree this is a likely source of the problem. See ["Why would a correct shell script give a wrapped/truncated/corrupted error message?"](https://stackoverflow.com/questions/31885409/why-would-a-correct-shell-script-give-a-wrapped-truncated-corrupted-error-messag) and ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) for more info and options. – Gordon Davisson Mar 12 '18 at 00:40