I wrote a shell script which opens some directories and runs some script, I run this bash file by terminal (bash filename.sh), how can I make it clickable?
You need to add the following shebang line to the top of your code.
#!/bin/bash
You also need to ensure that the script has executable permissions by running:
chmod a+x <filename>.sh
You first need to start your script with
'#!/bin/bash '
and save it as <filename>.sh Also make sure that you keep the permissions as a+x i.e all users can execute the script.
Related
I am trying to learn the basics of shell. I used vim editro for creating my own list of commands to be executed. Here is the way I created the code
vi mycommands
then inside this file I wrote
cd Documents
I am using macOS Catalina which has zsh by default but switched to bash
So when I write the following command in the terminal:
$ sh +x mycommands
It shows
+cd Documents
The Documents has some files and directories but it is not changing directory.Where am I going wrong?
Any help will be greatly appreciated.
Scripts run like sh myscript execute in a separate sub-shell, not the current shell. Changing directory inside a script will not cause your shell to change directory. If you want to change directory in your shell, you need to run the commands in your shell.
To do that, run:
. ./myscript (sh, bash) or source ./myscript (bash).
See this question.
I'd like to make some files to put in my /usr/bin folder (Mac OS) and be able to run them by typing the name to the terminal, like the commands gcc, cd, or vim (those are all in that folder). When I open such a file as text, it appears encrypted, so I'm not sure how to create one or what extension to use. Thank you.
The files you see in /usr/bin are not encrypted - they're compiled code in machine language different from bash scripts. You can however have scripts also in the /usr/bin location and have them run exactly as you are expecting.
In order to do that, you will have to create an executable script file. In unix, scripts are not identified by file extension but by 2 things:
executable bit set on the file permission level
Script interpreter header, such as #!/bin/bash
For a bash script, you can do the following:
Make a new file with the following content:
#!/bin/bash
echo "Hello world - My first bash script."
Save the file as hello.sh (the .sh is just convention, it could be any file name).
Then run chmod +x hello.sh and you will be able to run this file as an executable.
Move this file to /usr/local/bin and you should be able to run hello.sh from command line and it should execute your program.
You can create scripts, for example in bash, make the file executable with chmod and put the file path in your $PATH.
Example:
Create a new file called myscript.sh, you can use vi or any editor you prefer.
Add the content below on it:
#!/bin/bash
echo "Hello world!"
Now make it executable:
chmod u+x my script.sh
You can run your script like this ./myscript.sh or add it to your path:
export PATH=$PATH:$PWD
Now you can just run with the name, like myscript.sh
Programs such as gcc and cd are not encrypted, but compiled; however, if you create a shell script (without .sh as suffix; the .sh file extension for a shell script is optional) and place it in /usr/bin or any PATH location, and you chmod +x <script-path> (to give execute permission) then you can directly use that as a command.
Note: use shebang to mention the script interpreter, e.g. #!/usr/bin/env bash or equivalent for korn shell, python, etc.
I'm trying to make a script that can be invoked in the following fashion:
"run foo"
The script will run with the command line argument foo. Can this even be done with a shell script? The best I can do is "sh run.sh foo". Thanks for the help!
Make the file run a copy of, or link to, your run.sh (or simply move run.sh to run).
Make the file run executable (chmod 755 run or more restrictive, but the owner permissions must be at least 5).
Make sure run is in a directory on your PATH (e.g. $HOME/bin if that is on your PATH; if you don't have $HOME/bin on your PATH, create the directory and add it to your PATH in your profile or other startup scripts).
Then you can type:
run foo
Note that if you want the script run with /bin/sh, make sure the first line of run is #!/bin/sh.
Name the script run and place it into a directory in $PATH. Also make sure it has executable permissions and shebang.
$PATH is a special variable that holds the path the shell will search when executing a command without path.
mv run.sh /usr/local/bin/run
run foo
I have a shell script with some functionalities. I want to convert it to an executable file. Any idea if this is possible?
Thanks
Add the following line at the very top of your script:
#!/bin/sh
This is known as a shebang. It indicates which program to invoke the shell script with when you execute it. You could change it to anything. Eg, to run a zsh script you would use #!/bin/zsh, which is the path to the zsh interpreter on my machine.
Then you need to mark the file as executable using chmod. You can do this as follows:
chmod +x myscript
You can now run the script like this:
/full/path/to/myscript
Or, if you're in the directory the script is in:
./myscript
The '.' expands to the path of your current working directory.
If you want to be able to run the script from anywhere, stick it somewhere in your path. Eg.
mv myscript /usr/bin
You can now run the script from anywhere by typing in just the name.
i m creating one application on ubuntu server in shell script. I write one shell script which runs other perl scripts. i want .exe file of that .sh file
You're just trying to write a shell script. In that case you just need to create the .sh file you want to be executable and then write your shell script as follows (the #!/bin/sh indicates which interpreter to use)
#!/bin/sh
... shell commands ...
then do a
chmod +x myscript.sh
to make it executable. Then to run it you perform a
./myscript.sh
If you want to have you application installed via yum, you should package it as an rpm, put it in a public repository and then make it available to your users.