.sh can only be executed by explicitly typing bash [closed] - linux

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
When I run sudo ./hello.sh form command line, it says:
: command not found
but when I explicitly type bash like :sudo bash ./hello.sh
it run successful,why?

At the first line of your Bash Script you should use shebang.
for your script shebang is like below:
#!/bin/bash
After that your script should be executable. use this command:
chomd +x hello.sh
now run your script.
sudo ./hello.sh

Related

Bash script returning command not found, when setting the PATH [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am very new to Linux and Bash scripting. Also my first question on stackoverflow.
I am trying to create a bash script which I want to use from any directory.
So far this is what I did
Created a simple bash file first
#!/usr/bin/bash
echo "This is a bash script"
exit 0
I set the permissions for execute using chmod +x myfilename.sh
And then I edited the .profile file under ~/.profile
Added the line "export PATH="$PATH:$HOME/bash_course/scripts"
After that I ran the command source ~/.profile
Now I tried to run myfilename from the terminal, but it returns Command not found.
Any idea what could have gone wrong?
I checked the path of my bash and it is /usr/bin/bash
If I run ./myfilename.sh from file path, it is working. But I am trying to run from other directories.
FYI : I am doing all of this on a WSL
Found the issue. My path was actually /bash-course/scripts and not /bash_course/scripts
I corrected the exported path under .profile
Thanks everyone

Linux which cd worked. But cd is a builtin command in bash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I just learned Linux, and read the book "The Linux Command-Line". It says:
cd is a builtin command in bash.
So we cannot find anything with which cd; but somehow it worked well in my computer:
$ which cd
/usr/bin/cd
It is because I use CentOS?
For shell builtins use help rather than man. help cd will give you usage information. which is misleading since it only finds binaries. Use type.
$ type cd
cd is a shell builtin
$ help cd
cd: cd [-L|[-P [-e]] [-#]] [dir]
Change the shell working directory.
...
Now as it happens, there is a useless binary* /usr/bin/cd on your system. It's useless both because the shell builtin supercedes it, and because it's impossible for a binary to change the directory of the parent shell. Try to use it and you'll find it does nothing at all.
/dir1$ /usr/bin/cd /dir2
/dir1$

how do I basic script with linux? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
what does this command do?
!/bin/bash
My first script
clear
echo (I don't know what will come after echo , can you help me with that too?)
./hello.shell
#!/bin/bash is called the shebang (you missed the leading #).
It tells which program will execute your script.
clear is for clearing screen.
echo outputs following argument to the standard output (your terminal by default). But you must not surround your string with parenthesis as it's used for grouping command in a sub-shell. If you want to print (...), you'll have too use double quotes :
echo "(I don't know what will come after echo , can you help me with that too?)"
./hello.shell will execute your script after you gave it execute permissions with chmod +x hello.shell.
Note that commonly used extension for a shell script is .sh rather than .shell.
For more, try theses links :
http://mywiki.wooledge.org/BashGuide/
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
#!/bin/bash tells to the SO that this file is a script and that bash is the shell that must execute it. So you can found: #!/opt/bin/perl for perl scripts, #!/bin/csh for c-shell, #!/bin/zsh ...

How to write shell script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to Linux commands. I have a requirement where I want to start Tomcat using a shell script. The location of startup.sh file is in /usr/lib/apache-tomcat-7.0.14/bin/. Tomcat is starting using command sh startup.sh. I want to create a shell script so that it will go to that folder and will execute sh startup.sh command. How can I do this using a shell script? Can anyone share the script for doing this?
Are you serious?
#!/bin/bash
cd /usr/lib/apache-tomcat-7.0.14/bin/
sh startup.sh
#!/bin/bash
exec /usr/lib/apache-tomcat-7.0.14/bin/startup.sh
is simpler (as does not have a shell kicking around)

Linux shell script erroroing out due to /usr/bin/sh [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to run a shell script on redhat linux to install an app. I am getting an error saying /usr/bin.sh: bad interpreter: no such file or directory.
In the shell script the script begins with:
#!/usr/bin/shBUILD_ID=$1.....
I am just trying to understand what the path at the begining of the line is for? Is that a directory it looks for to deploy the app?
Thanks
The first line should be #!/usr/bin/sh or #!/bin/sh if its a shell script.
If the first line are #!/usr/bin/sh then try to see if /usr/bin/sh exist and you with ls -l /usr/bin/sh
If you cant find sh then your system are in a bad stat.
The #! is a magic number that tells the kernel that the file is an excutable script.
The string immediately following the #! is the path to an interpreter that is called to read and execute the contents of the file. In the line
#!/ust/bin/shBUILD_ID=$1.....
the interpreter is /ust/bin/shBUILD_ID=$1..... The interpreter is read directly, with no shell variable substition, so it will look for a file exactly as you specified (including the equal sign, dollar, dots etc.). If the interpreter you specified is not found, a default shell issues an error message, and yours looks totally wrong. Try #!/bin/sh.
If the interpreter string is followed by a space and then some arguments, those arguments are passed to the interpeter when it is invoked.
See for example http://bash.cyberciti.biz/guide/Shebang

Resources