I am new to linux, and I have a shell script (.sh) file on my Desktop that I want to run.
These are the steps that I did:
This is the content of the test.sh file on my Desktop:
#!bin/bash
#test.sh
echo "test"
I want to run (Execute) test.sh through the terminal. These are the commands that I'm using:
cd Desktop
I give permission to run test.sh with:
chmod +x test.sh
and then try to open the file:
test.sh
But I get this error:
test.sh: command not found
What am I doing wrong?
Your shell will search the directories in your $PATH environment variable for executable files.
If the current directory is not in it (and your Desktop directory won't be, by default), you must specify the path explicitly.
./test.sh
bash: ./test.sh: bin/bash: bad interpreter: No such file or directory
Replace:
#!bin/bash
With:
#!/bin/bash
Source
try running the script like this: ./test.sh
Do like this:
FIRST WAY:
bash test.sh
or
sh test.sh
Related
Why can't I run a bash script in the current directory I'm in?
Whenever I run the script the commands are executed in the home directory.
The only answers I found are included below.
I do use the zsh shell. I don't know if that changes anything.
Thanks in advance!
What I have tried so far:
#!/bin/bash
touch test.txt
#!/bin/bash
cd $PWD
touch test.txt
#!/bin/bash
variable = $PWD
cd $variable
touch test.txt
#!/bin/bash
variable= pwd
cd $variable
touch test.txt
#!/bin/bash
cd -
touch test.txt
If I run the script for example from /home/user/dir1/dir1.1 the test.txt file is created in the home directory (/home/user) and I get redirected to the home directory as well.
in bash there are two things to do:
ensure that the shell script file is saved properly and is chmod'd to be an executable.
To do so, save the file (e.g. script.sh) with the code you want, and then run chmod +x script.sh to make linux understand that this file is an executable.
call the executable properly using the ./script.sh command. alternatively, you can also call the script from remote folder by calling it using the absolute path the script is in (e.g. /folder/folder/folder/script.sh).
This should execute the file. from there, it's about your code and if you need help there, please update your question.
I have a problem with a script ksh (mainScript) that call other scripts.
mainScript:
call ./folder1/folder2/first.sh
error that I get:
./folder1/folder2/first.sh not found [No such file or directory]
I verify with ls -l /folder1/folder2/first.sh and I see that the script exist.
Thank you for helping me.
remove the . from your path and also provide execute permission on the file then you will be able to run the script. like
[root#localhost#] chmod +x /folder1/folder2/first.sh
then run
[root#localhost#] /folder1/folder2/first.sh
Also, you can run your script using following:
ksh /folder1/folder2/first.sh or sh /folder1/folder2/first.sh or bash /folder1/folder2/first.sh etc
The following are two shell scripts stored in the same folder with execute permissions on both:
shell1.sh
#!/bin/bash
exec shell2.sh
shell2.sh
#!/bin/bash
pwd
When trying to execute shell1.sh I am getting the following error:
./shell1.sh: line 3: exec: shell2.sh: not found
Is there something I am doing incorrectly? This works in other machines though but just in one particular server its not working.
Any suggestions would be helpful.
The current dir is not part of your PATH.
Try
exec ./shell2.sh
I am quite new to the shell scripting.
So I am writing the shell script to list all files available in the directory using ls command.
but I am getting the error bad interpreter: Permission denied
#!/home/gaurav
echo "Welcome bash shell scripting"
ls
echo "this complets the listing of directories"
I want to get the list of "/home/gaurav" this path
Thanks
This line...
#!/home/gaurav
... means "instead of using /bin/bash, use /home/guarav as the program to run this file". This is not what you want. What you want is either:
cd /home/gaurav # at the top, or
ls /home/gaurav # between echoes
Problem is this line:
#!/home/gaurav
This is called shebang and it should be the bash/shell interpreter like this:
#!/bin/bash
one that interprets and executes your script. Since /home/gaurav is not a valid interpreter you're getting that error.
You probably want this in your script:
ls /home/gaurav
to list all files/directories in /home/gaurav path.
Either add #!/bin/bash or #!/bin/sh instead of #!/home/gaurav line while starting script.
Because, while running shell script, you have to give path of which bash or sh are you going to run to execute that script.
I wrote a linux command and it runs perfectly in command line:
/bin/netstat -an | grep '3306' | sed 's/.*/[MYSQLMON]&/' > /home/bbWifiExt/logs/WIFIMonitor.log
however when I copy this code to .sh and run the .sh file i got:
No such file or directory
Can anyone tell me why? Many thanks.
You must either call it as
sh mycommand.sh
or make your shell script executable. Insert #! /bin/sh or #! /bin/bash as the first line and
chmod +x mycommand.sh
before calling
mycommand.sh
for my situation (rename and copied file from windows to linux) the solution was :
dos2unix script.sh
If the first line of the script something like
#!/bin/sh
and the execute bit set i.e.
chmod +x script.sh