How do you run bash script as a command? - linux

I have a bash script, which I use for configuration of different parameters in text files in my wireless access media server.
The script is located in one directory, and because I do all of configurations using putty, I have to either use the full path of the file or move to the directory that contains the file. I would like to avoid this.
Is it possible to save the bash script in or edit the bash script so that I can run it as command, for example as cp or ls commands?

The script needs to be executable, with:
chmod +x scriptname
(or similar).
Also, you want the script to be located in a directory that is in your PATH.
To see your PATH use:
echo $PATH
Your choices are: to move (or link) the file into one of those directories, or to add the directory it is in to your PATH.
You can add a directory to your PATH with:
PATH=$PATH:/name/of/my/directory
and if you do this in the file $HOME/.bashrc it will happen for each of your shell's automatically.

You can place a softlink to the script under /usr/local/bin (Should be in $PATH like John said)
ln -s /path/to/script /usr/local/bin/scriptname
This should do the trick.

You can write a minimal wrapper in your home directory:
#!/bin/bash
exec /yourpath/yourfile.extension
And run your child script with this command ./NameOfYourScript
update: Unix hawks will probably say the first solution is a no-brainer because of the additional admin work it will load on you. Agreed, but on your requirements, my solution works :)
Otherwise, you can use an alias; you will have to amend your .bashrc
alias menu='bash /yourpath/menuScript.sh'

Another way is to run it with:
/bin/bash /path/to/script
Then the file doesn't need to be executable.

Related

In my shell script I would like to list all the files in my current directory

In my shell script I would like to list all the files and directories in my current directory.
I know the command is ls, but I have no idea how to run it in a shellscript.
Thanks for anyhelp.
So you're basically asking: what is a shell-script, how do I create one, and how do I run it ....
Use your editor of choice to create a file, give it the following content:
#!/bin/sh # change to your preferred shell, sh being a low common denominator
command # in your immediate question that would be `ls`
Save the file.
Run chmod u+x file (not the word file, but what you called your saved script).
Then you can execute your file like so:
./file

Add a bash script to path

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc

What does it mean in Linux for installing script

I am trying to install some scripts into linux and follwoing line is given as instruction.
Install the xyz script into some convenient directory in $PATH.
but I'm unable to understand what exactly does it mean.How do I install given script in $PATH directory.Script is placed under /users/username/ Dir.
In a terminal type echo $PATH. You will see a list of directories. Put your script, or a link to your script in one of those directories, typically in /usr/local/bin.
its a bad description of the script-author :) usually, in your $PATH are multiple directories mentioned, separated by colon.
you can echo them:
echo $PATH
What the Author means: just copy the script in a directory which is in your $PATH, e.g. /usr/local/bin
$PATH is a list of directories where bash looks for executables.
The given instruction suggests that yours scripts should be put in one of these directories.
An alternative is to put your scripts in any directory and add that directory to $PATH. In your case, add the following line in your $HOME/.bash_profile configuration file:
export PATH=$PATH:/users/username

How to copy a .sh file to 'bin/sh/'

I am trying to copy a .sh file into 'bin/sh' directory, but I am not able to access 'sh'. Is there any way or command to copy .sh file to 'sh' directory?
If you make your own sh script and want it to be found and executed from your shell prompt you:
Change rights for the script file myscript.sh
chmod +w myscript.sh
or
chmod 755 myscript
etc.. depending on who is allowed to execute and change the script.
You can copy the file to /bin/ (not recommended) or /usr/bin (not as bad, and the place to put it possibly if you have more users than yourself on the system) or you add a new path where you keep your sh scripts into the PATH environment. If you run bash it is in ~/.bashrc.
You can test this from the prompt before changing .bashrc if you (example in bash) do
export PATH=$PATH:~/myscripts/
For scripts you have put in your own directory myscripts.
To put it into /usr/bin you do
sudo cp myscript.sh /usr/bin/.
If you do not have any sudo rights, you are bound to the latter solution with a directory of your own where you put your sh files and changing the PATH.
Recommended to study: read up on the man pages for chmod and bash. Also on changing environment variables.

command not found in bash-3.2$

I tried running a script file using bash but it showed an error
bash-3.2$ example.sh : command not found
I also tried
ls -l example.sh
I found that it was not executable, so I used
sudo chmod 777 example.sh
I again tried running it but same error was coming. I double checked that I am in the same folder as the file using ls. But still I am not able to execute the script file.
I finally tried making a dummy script file and running it , and found the same error
I think there is some problem with BASH. Can some one help me with what is the problem?
I am working on redhat, bash was already installed in my system
Since I am newbie on linux any help would be appreciated
bash search for commands in your $PATH. Apparently the current directory, ., is not in your $PATH. (This is a good thing; having . in your $PATH is insecure.)
You'll need to specify a directory name. Just type:
./example.sh
Incidentally, doing:
sudo chmod 777 example.sh
is two kinds of overkill. First, you don't need to use sudo; use sudo only when you actually need to. Presumably your personal account owns the file, so you can just use chmod directly.
Second, 777 is way too permissive. It allows anyone on the system to read, execute, or modify example.sh. (If you're the only person on the system it may not matter much, but it's still a bad habit.) Typically you should use 755 for directories and for files that need to be executable, and 644 for files that don't need to be executable.
Or just use
chmod +x example.sh
to set execute permission (your umask will prevent that from setting the permissions too loosely).
. (the current directory) is probably not on your path. Try ./example.sh or bash example.sh. You could also add . to your PATH environment variable, but that's generally frowned upon.
Your bash PATH probably doesn't include ., try running it by typing:
./example.sh
When you type a command, your shell searches your path to try to find the command, if the current directory (e.g. .) isn't part of the path, the script that you are trying to run won't be found. You'd have to explicitly give it the path to where this command is. And since it's in your current directory, you can just add ./ in front of the command.
first confirm the bash path
to check the path of bash use:
which bash
if you get "/bin/bash"
then add
#!/bin/bash
...
...
or whatever is the path on first line of your bash script

Resources