0

This probably quite basic but I have spent whole day finding an answer without much success.

I have an executable script that resides in ~/Desktop/shell/myScript.sh

I want a single line command to run this script from my terminal that outputs to a new directory in same directory where the script is located no matter what my present working directory is.

I was using:

mkdir -p tmp && 
  ./Desktop/shell/myScript.sh|grep '18x18'|cut -d":" -f1 > tmp/myList.txt

But it creates new directory in present working directory and not on the target location.

Any help would be appreciated.

Thanks!

Jay Zee
  • 1
  • 1
  • 2
    There is no way to run `mkdir -p tmp && somescript` and have `somescript` affect where the directory is created. You could rewrite the script so that you can run `somescript tmp/myList.txt`, and have the script [look up its directory](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) and write the file. – that other guy Dec 03 '14 at 00:15
  • Thank you for the reply. Lets ignore the mkdir part and just output in the script directory and not the present working directory, what are my options? – Jay Zee Dec 03 '14 at 00:24
  • There is similarly no way to run `somescript > tmp/myList.txt` and have `somescript` affect where the file is written. As mentioned, you can rewrite the script so that you can run `somescript tmp/myList.txt`, and then put e.g. `echo text > "/my/dir/$1"` in the script to write to `/my/dir/tmp/myList.txt` – that other guy Dec 03 '14 at 00:31

3 Answers3

1

You could solve it in one line if you pre-define a variable:

export LOC=$HOME/Desktop/shell

Then you can say

mkdir -p $LOC/tmp && $LOC/myScript.sh | grep '18x18' | cut -d":" -f1 > $LOC/tmp/myList.txt

But if you're doing this repeatedly it might be better long-term to wrap myScript.sh so that it creates the directory, and redirects the output, for you. The grep and cut parameters, as well as the output file name, would be passed as command-line arguments and options to the wrapper.

jez
  • 14,867
  • 5
  • 37
  • 64
1

How about this:

SCRIPTDIR="./Desktop/shell/" ; mkdir "$SCRIPTDIR/tmp" ; "$SCRIPTDIR/myScript.sh" | grep '18x18' | cut -d ":" -f 1 > "$SCRIPTDIR/tmp/myList.txt"

In your case you have to give the path to the script anyway. If you put the script in the path where it is automatically searched, e.g. $HOME/bin, and you can just type myScript.sh without the directory prefix, you can use SCRIPTDIR=$( dirname $( which myScript.sh ) ).

Mixing directories with binaries and data files is usually a bad idea. For temporary files /tmp is the place to go. Consider that your script might become famous and get installed by the administrator in /usr/bin and run by several people at the same time. For this reason, try to think mktemp.

ua2b
  • 182
  • 3
1

YOUR SCRIPT CAN DO THIS FOR YOU WITH SOME CODES

Instead of doing this manually from the command line and who knows where you will move your script and put it. add the following codes

[1] Find your script directory location using dirname

script_directory=`dirname $0`

The above code will find your script directory and save it in a variable.

[2] Create your "tmp" folder in your script directory

mkdir "$script_directory/tmp 2> /dev/null"

The above code will make a directory called "tmp" in your script directory. If the directory exist, mkdir will not overwrite any existing directory using this command line and gave an error. I hide all errors by "2> /dev/null"

[3] Open your script and modify it using "cut" and then redirect the output to a new file

cat "$0"|grep '18x18'|cut -d":" -f1 > "$script_directory"/tmp/myList.txt
repzero
  • 8,254
  • 2
  • 18
  • 40