0

How to make a script that will make another script in the same folder executable? I don't want to worry about path since it will be in the same directory. So it should be something like(i'm trying to make D3GO script executable):

#!/bin/bash    

chmod +x "$pwd"/D3GO

I'm very new to scripts so pls don't laugh :D Thanks!

2 Answers 2

2

When you run a bash script, all of it's commands run in the directory where the script is invoked from, including pwd. For example:

#!/bin/bash

pwd

If you run this from /whatever/path, it will display /whatever/path, no matter where the script is placed.

If you want to operate on files in the directory where you are running the script from, you can omit pwd like this:

chmod +x D3GO

This will be interpreted as chmod +x /whatever/path/D3GO if you run it from /whatever/path, no matter where the script is actually placed.

To operate in the directory where the script is located, you can use the $BASH_SOURCE variable. It is an array, and it's first element is the path to the script itself (i.e. /script/directory/myscript.sh). You can combine this with the dirname command to get just the directory:

chmod +x `dirname ${BASH_SOURCE[0]}`/D3GO

This will look for D3GO in the directory where this script is placed instead of where it's invoked from. (Thanks to @muru for pointing out my mistake.)

0
1

You were super close!

#!/bin/bash    

chmod +x D3GO
3
  • You were super close! But this is not what OP asked for. This will look for a file named D3GO in the directory you run the script from, not the directory of the script. It will only work as expected if you run the script from it's own directory.
    – kraxor
    Jul 8, 2014 at 18:14
  • @kraxor, so how does using pwd improve things? It would still look for a script in the directory your running it from.
    – muru
    Jul 8, 2014 at 18:28
  • @muru you are right, and I was wrong, but for the right reasons. I've updated my answer.
    – kraxor
    Jul 8, 2014 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .