How to run a script everywhere in linux - linux

I have a script HOST_AVAI.sh in /home/campus27/zwang10/bin. And in my .bashrc, I add export PATH=$PATH:/home/zwang10/bin/HOST_AVAI.sh. But after I type HOST_AVAI.sh, it shows HOST_AVAI.sh: Command not found..
Can someone help me here?
ADDED
$ echo $SHELL
/bin/tcsh

Your path should be PATH=$PATH:/home/zwang10/bin and add this in .bash_profile. After this run the script with following command :
$ . .bash_profile
Make sure your HOST_AVAI.sh must have the execute permission.
$ cd /home/zwang10/bin
$ chmod +x HOST_AVAI.sh
now run this command from anywhere.

Related

How to Start a Bash Script with Fewest Keystrokes

To fire a shell script from the command line, instead typing this at a Linux/Unix command line:
~$ <shell> tale.sh
In my case, using bash:
~$ bash tale.sh
How do you setup profile/defaults/scripts so that the command will run with just:
~$ tale
I know this can be different for different shells. I need the answer for bash.
This is the whole listing for ~/tale.sh:
#! /bin/bash
tail -f ~/lp/_logs/error.log
That file, ~/lp/_logs/error.log, is a PHP error log.
First, you need to rename your script:
mv tale.sh tale
Then, there may be two additional steps:
1) Set the executable bit on your script:
chmod +x tale
2) Make sure your script is in your PATH. For example, you could place it in your bin directory (assuming your bin directory is in your path):
mv tale ~/bin
Once all this is in place your script will run from anywhere, whichever shell you are using.
There is an alternative approach called "alias" which could be use.
alias tale="./tale.sh"
or
alias tale="tail -f ~/lp/_logs/error.log"
This lasts until session is not terminated. It can be persists by making an entry in .bashrc or .bash_aliases file.
You can try with the below one line command :)
chmod +x tale.sh && bash tale.sh;

Update .bashrc from provisioning shell script with Vagrant

I'm trying to add some additional lines to .bashrc in my home directory from the provisioning shell script when launching a new instance with Vagrant.
In the shell script I have:
set -x
sudo apt-get update
sudo apt-get install vim
echo "source /usr/local/share/chruby/chruby.sh">> ~/.bashrc
echo "source /usr/local/share/chruby/auto.sh">> ~/.bashrc
However after completion nothing has been written to .bashrc.
This is a cut down version of the full script the intention of which is to install Ruby/Rails.
You need to give the full path to the file.
E.g.
echo "source /usr/local/share/chruby/chruby.sh" >> /home/vagrant/.bashrc
Add these lines to .bashrc
if [ -f /usr/local/share/chruby/chruby.sh ]; then
. /usr/local/share/chruby/chruby.sh
fi
It will textually include the script into .bashrc and execute it when opening a new shell.
Try this for your last 2 lines - it should give you exactly what you need.
echo "source /usr/local/share/chruby/chruby.sh" >> /home/vagrant/.bashrc
echo "source /usr/local/share/chruby/auto.sh" >> /home/vagrant/.bashrc

Shell script file (.sh) does not run, and throws an error

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

What is the difference of running commands in terminal and shell script?

I want to check if tmux alias exists, when I run command in terminal:
$ type -t tmux
the result is
$ alias
But when I put "type -t tmux" in a shell script and run, the result is
$ ./test.sh
$ file
Why the result is different ?
My test.sh is:
#!/usr/bin/env bash
set -e
type -t tmux
Any aliases defined in .bash_profile should be read and respected by tmux, but does not read anything in .bashrc.
Invoking test.sh is a sub-process, and does not use environment from current process unless you source it
source test.sh but that also allows that script to modify current environment.

executing shell command using shell script

I have shell command ./cherrypicker.sh input.txt which works fine in terminal.
But I want to execute few more command before and after this command like
echo "some text" > input.txt
./cherrypicker.sh input.txt
result < input.txt.response
rm input.*
So I put all this in another shell file, alls.sh and tried to execute it like this
./alls.sh
which says
bash: ./test.sh: Permission denied
then
sudo ./alls.sh
which gives
sudo: ./test.sh: command not found
what is the correct way?
Add execution rights to the script:
chmod +x test.sh
The second problem is related to the path. cd to the directory or use the full path (use whichever is more appropriate for the task).
You might want to make sure that test.sh is actually executable by doing
chmod 0700 /path/to/test.sh
And then run it without sudo.
A note on sudo: it is not muckrake to get all your problems out of your way. ;) Think of it is rather a foil to punctually and elegantly make your point clear. ;)

Resources