Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
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 yesterday
    
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 –  Alexander yesterday
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 yesterday

5 Answers 5

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.

share|improve this answer

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

sh /path/pycharm.sh
share|improve this answer
    
I need it called thru another bash file. –  Alexander yesterday
    
Yes, just remove ./ from the script and it should work. –  zer0rest yesterday
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 yesterday
    
@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 yesterday

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.

share|improve this answer

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.

share|improve this answer

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
share|improve this answer
    
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 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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