0

I need to work with different files in the script. The easy and convenient way to do it is to use "Drag and drop". But.

Drag and drop files to a bash script causes "Permission denied" when you output data to a file.

I use git-bash on Windows 10.

#!/bin/bash
echo "123" > out123.txt
sleep 5

When I run script in common way (I double click on it) it works well.

When I drag and drop a file (from the same folder) on the script:

#bash: out123.txt: Permission denied

Upd: It's for people who came from google:

How to get dropped data within bash script.

It's usual argument. Code example [1] or [2]:

for var in "$@"
do
    echo "$var"
done
KeyKi
  • 2,693
  • 3
  • 12
  • 24
  • Add `pwd` to your script. What does it output? – melpomene May 08 '19 at 08:51
  • When I click on it `/c/folder` When I drag and drop on it `/c/WINDOWS/system32` – KeyKi May 08 '19 at 08:55
  • So the script is run from a different directory. You're getting a "permission denied" error because the script is trying to create a file in `C:\Windows\system32`. – melpomene May 08 '19 at 08:58
  • Yes. Thanks for it. I need to parse the input data and change working directory via `cd` – KeyKi May 08 '19 at 09:00
  • The solution is `cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"` The main part of the code is from [here](https://stackoverflow.com/questions/59895/). – KeyKi May 08 '19 at 09:27
  • That's crazy overcomplicated. In particular, `cd "$( cd ... && pwd )"` is redundant. I'd just use `cd "$(dirname "$0")"`. – melpomene May 08 '19 at 09:35
  • Thanks second time. I have tried `"\`dirname $0\`" ` from the other comment, but it does not work with the path that contain a space (.../new folder/...). Yours is working. – KeyKi May 08 '19 at 09:43

0 Answers0