4

I have the following script:

#!/bin/bash

if test -f "/path/pycharm.sh"; then sh ./pycharm.sh;
fi

I am trying to run pycharm.sh from a bash file and I've looked carefully to give all the permissions needed to the file. Unfortunately every time I run it I get this:

Can't open ./pycharm.sh

  • 1
    What directory are you running the script from? Is it the same directory that pycharm.sh is in? Is that the entire error message? – terdon Apr 14 '15 at 11:59
  • No, it's not in the same directory. And yes, that's the entire message that I get. I need that script to be on my desktop so it won't help me if I put it in the same directory as pycharm.sh – Cajuu' Apr 14 '15 at 12:00
  • 1
    It's strange that you use for the access to the file two different paths; once /path/pycharm.sh (absolute) and once ./pycharm.sh (relative). If it's the same file then the path definitions should match. – Janis Apr 14 '15 at 12:02
17

You don't use ./ to run a script in general, you use it to run a program (script or compiled binary) in the current directory.  If the second script is in /path/pycharm.sh, then you should run it as /path/pycharm.sh, and not ./pycharm.sh.

6

Your shell script is in another directory, use the absolute path.

sh /path/pycharm.sh
  • I need it called thru another bash file. – Cajuu' Apr 14 '15 at 12:02
  • Yes, just remove ./ from the script and it should work. – zer0rest Apr 14 '15 at 12:04
  • 1
    @zer0rest; if you omit ./ then it's also assumed to be in the current directory. Whether you call it sh ./somefile or sh somefile, it's the same. There's only a differene if somefile is chmoded as executable, then the PATH definition would matter in those two cases; the one without ./ would be searched through PATH. – Janis Apr 14 '15 at 12:08
  • @Janis; You're correct, I edited my answer. I just find it odd to run a script as sh ./script instead of just sh script. – zer0rest Apr 14 '15 at 12:10
5

The error is fairly explicit:

  • It either doesn't exist at the specified path, or
  • It doesn't have the permission to open it.

As you're using a relative path, I'd put my money on the first. Specify a full path to your second pycharm.sh and it should work.

0

Please execute pwd command and check that file pycharm.sh is present in that directory.Or you can provide full path in a script.

You can have more detailed information by using option -x while executing script.

-1

Am I missing something or do you mean to execute the very string whose existence you are checking first. If so, then the correct way is this:

#!/bin/bash

if test -f "/path/pycharm.sh"; then /path/./pycharm.sh;
fi
  • What do you think you are adding to the multiple correct answers that have already been posted?  And why do you suggest inserting /. into the second pathname? – G-Man Apr 14 '15 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.