how to run an exe using shell script

10.7k Views Asked by At

I have an exe which takes 2 parameters both are csv as below splitline.exe /dir/file1.csv /dir1/file2.csv, this basically splits the lines in the input file to 2 lines in the output file.So,I wrote a shell script to execute this exe. the directory is fixed,but the filename can differ, both filename in src directory and destination directory should be same.I am running it on windows environment.It is having MKS installed,so unix and shell script also can be executed. Below is the code snippet which I wrote:

#!/bin/sh
 srcdir='D:/srcdir/srcdir1/'

 destdir='D:/destdir/destdir1/'

 extension='csv'

srcfile='${srcdir}/*.csv'

if [[-f ${srcfile} in ${srcdir}]]
  then
   cSplit.exe "${srcdir}/{srcfile}.${extension}" "${destdir}/${srcfile}.${extension}"
    mv "${srcfile}" "${srcdir}/old"
else
    echo "no file"
fi

output: [[-f : splitty.sh 21 not found Its giving output as "no file" Please correct my mistake,as I am new to shell script

2

There are 2 best solutions below

15
Bach Lien On

How about this template:

#!/bin/bash
src="D:/srcdir/srcdir1"               # no '/' at the end of dirname
dst="D:/destdir/destdir1"
cd "$src" || exit                     # do nothing if $src is not a directory

for f in *.csv; do
  if [[ "$f" = '*.csv' ]]; then
    echo "no files"
    break
  fi
  if [[ -f "$f" ]]; then
    echo cSplit.exe "$f" "$dst/$f"    # use 'echo' for testing
    echo mv "$f" "$src/old/."         #   aka dry run
  fi
done

Note 1: If you are using Windows, then using / is OK in bash; but the command cSplit.exe may not accept / as directory delimiter. I am not using Windows, so I cannot check that.

Note 2: I do not really know Windows, so I tried NOT to use other commands like basename, or find, etc. As for drive name D: please check. Maybe it is mapped to /mnt/d or something.

Note 3: I've fixed the script based on Charles comments.

0
Aaron On

I would use something along those lines :

#!/bin/bash
cd 'D:/srcdir/srcdir1/'
find . -maxdepth 1 -name '*.csv' \
  -printf 'Handling %f...' \
  -a -exec cSplit.exe {} 'D:/destdir/destdir1/{}' \; \
  -a -exec mv {} ./old/ \; \
  -a -printf 'done\n'

For each .csv file in the D:/srcdir/srcdir1/ directory this will do the following actions in order :

  • Start writing Handling <file name>... on a line
  • Execute cSplit.exe with the first argument being a (relative) path to the file and the second a path to a file with the same name located in the D:/destdir/destdir1/ directory
  • Move the file from the D:/srcdir/srcdir1/ directory to the D:/srcdir/srcdir1/old directory
  • Write one followed by a linefeed on the end of the line.

The actions are joined with -a "AND" operands and will not be taken for a file if the previous action failed (a file for which cSplit.exe thrown an error won't be removed from the initial directory and backed-up, and the "done" will be missing from its line).

You will know no files were found if no output is displayed. If that's not enough for you you can add the following after the find command (needs to be on the same line as the end of the command) : | tee | grep '.' || echo 'no file'