Creating a command in linux - linux

I have created a simple script:
echo "the path of the current directory is `pwd`"
and saved it by the name pathinfo
then i have created a bin directory at my home page with path as
/home/vpnsadmin/bin
and copied my script(pathinfo) to that bin directory.
Now i want run this script as a command but it is showing error
-bash: /usr/bin/test2: No such file or directory
but if copy my script(pathinfo) to "/usr/bin/" then it runs as a command.
the PATH environment variable is set as-
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/vpnsadmin/bin
My question is why does the shell not run it as a command when it is present in /home/vpnsadmin/bin.
or else
why does it only check for the binary at /usr/bin and not at /home/vpnsadmin/bin or at /bin

The shell that is to execute your command needs to have the correct PATH variable set at the time of execution and, depending on shell, might need to have created its own internal (hash)map of the available commands.
Assuming you are using bash, try the following with your script saved in /usr/bin:
$ PATH=/ test2
$ PATH=/usr/bin test2
In the first case you should get an expected "not found" error, in the second it should work. The third test to perform is left as an exercise...
And I have to say that the supplied error message looks a bit odd if you actually tried to do
$ test2
and not
$ /usr/bin/test2
before copying the command to /usr/bin.
Edit:
Also, avoid naming your scripts test, in any way shape or form. This causes so much confusion for beginners.
Hint:
man test

Did you have the path to bash at the top of your script and did you use backticks around pwd?
#!/bin/bash
echo "the path of the current directory is `pwd`"
Did you make the file executable?
chmod +x pathinfo

There is another script pathinfo somewhere in your path which contains a call to /usr/bin/test2
Try whereis pathinfo to see how many there are and which pathinfo to see which one your shell currently prefers.

Related

Execute bash and sh file on Mac without writing extension

I have a .sh file and I want to execute it from shell without writing the extension.
What I did:
I created a directory and added it to $PATH
I gave to the file.sh chmod 711
and the file contain #!/bin/sh (I tried also bash).
However when I try to execute myscript without sh I get command not found
while if I try with myscript.sh I get the right result.
How could I do?
I read also: How to run a shell script on a Unix console or Mac terminal? and executing shell script without calling sh implicitly but no solution
Result of ls -l
ls -l /Users/Mitro/scripts
total 8
-rwx--x--x 1 Mitro staff 22 Nov 26 10:25 myscript.sh
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/Mitro/scripts
Two problems...
Firstly, file is already an executable program in /usr/bin that tells you the type of a file - i.e. whether it is an image, or a song or a database. Quick example:
file a.png
a.png: PNG image data, 1 x 1, 1-bit colormap, non-interlaced
So, file is a bad name for a shell script - likewise is test.
Secondly, if you want to execute a script or program in your current directory, also known as dot (.), you either need to have dot in your PATH, or you need to explicitly tell your shell that the file you want to run is in the current directory. The easier option is the second, which means if your script is called fred, you run it with
./fred
which tells the shell it is in your current directory.
The longer option, if you want to always be able to run scripts in the current directory, is to add dot to your PATH. So, you locate your login script (probably $HOME/.profile) and you find the line that sets your PATH and you add the current directory to it.
export PATH=$PATH:.
Once you have set that, you are best off logging out and back in to have it take effect.
Some folks disapprove of the idea of adding dot to their PATH - I don't. YMMV.
You can add alias. If you have /some/path/to/script.py, do:
alias my_script='/some/path/to/my_script.py'
Now when you enter my_script, your script would be executed.
For mac the profile file is ~/.bash_profile as opposed to ~/.profile which did not work.

I'm learning about shebangs. How do I make it work with node.js in a Mac terminal?

I have:
#!/usr/bin/env node
console.log("It works!");
I learned that env finds the node program and interprets it with node. I checked that env exists in /usr/bin.
When I call node itworks.js it works and outputs It works!. However, from what I understand, I should just be able to call itworks.js without node due to the shebang. But when I make this command it says -bash: itworks.js: command not found.
Could someone help me get the shebang to work?
First of all you need to make the file executable:
chmod +x itworks.js
Then you need to call it by specifying the path as well. Either:
/where/it/is/on/disk/itworks.js
or:
./itworks.js
The reason for :
-bash: itworks.js: command not found
is because bash looks for programs in directories in the PATH environment variable when you do not say where the file is - it does not look in the current directory unless you tell it.
You could update the PATH variable with the current directory shortcut ., but that can be a security risk, so most run the program like this:
./itworks.js
Of course if you put your scripts all in one directory then you could add that to PATH in one of your start-up files. For example, if you had a directory called bin in your home directory that held your scripts:
PATH=$PATH:"$HOME/bin"
You also need to add the execute permissions to the script:
chmod u+x itworks.js
The u indicates that we only give permission for the current user to execute this file. If we omit the u then anyone can run it.

Terminal cannot run shell script files stored in PATH directories

Getting "command not found" when trying to run shell script file. The file itself is stored in a directory, that is added to PATH, but still, terminal doesn't recognize it as shell script.
Here's the sequence I try:
tajimura/GAMIT% echo $PATH
/usr/local/bin:/usr/bin:/bin:/user/games:/usr/X11R6/bin:/usr/bin/X11:/usr/lib64/jvm/jre/bin:/home/tajimura/GAMIT/gamit/bin:/home/jaimura/GAMIT/kv/bin:/home/tajimura/GAMIT/com
tajirmura/GAMIT% ls /home/tajimura/GAMIT/com/sh_steup
/home/tajimura/GAMIT/com/sh_setup
tajimura/GAMIT% sh_setup
sh_setup: Command not found.
tajimura/GAMIT% sh sh_setup
sh: sh_setup: No such file or directory
tajimura/GAMIT% l /home/tajimura/GAMIT/com/sh_setup
-rwxr-xr-x 1 tajimura users 11109 Aug 20 2013 /home/tajimura/GAMIT/com/sh_setup
Here is a screenshot:
PS: Opensuse 12.1 here.
ADDED: I was executing it just fine during first 4 days, so I guess hashbang is not an issue. But I can't guarantee that workstation wasn't rebooted between my sessions, so maybe (just may be) -noexec is the cause. However, script sits in my home directory on hard disk, it's not a removable drive.
ADDED: The first five lines of sh_setup:
/home/tajimura% sed 5q /home/tajimura/GAMIT/com/sh_setup
#!/bin/csh -f
#
#doc Check and setup the GAMIT tables directory
#doc
#
Your script is either not executable (make it so with chmod +x sh_setup) or it specifies a broken (non-existing) interpreter on its hash-bang line (the first line of the sh_setup file, starting with #!).
Your sh sh_setup invocation fails because it doesn't use $PATH and you're in the wrong directory.
EDIT: Your script is clearly executable (I didn't spot this in your screenshot at first), which leaves us with a possibly incorrect interpreter.
I've found an unexpected solution of the problem. If I call the script under bash instead of csh everything is good.

call a shell script(command line tool) inside another shell script

I am using a tool called Droidbox for experiment.
The tool has a shell script droidbox.sh which I can invoke through terminal.
droidbox.sh takes one argument i.e path of the apk
Usage: ./droidbox.sh APK
I want to call the droidbox.sh through a shell script.
I wrote a shell script like
#!/bin/bash
ARG1="/home/xxx/a.apk"
/home/xxx/DroidBox_4.1.1/droidbox.sh "$ARG1"
I am getting error which says
python: can't open file 'scripts/droidbox.py': [Errno 2] No such file or directory
Can anybody point out what am I doing wrong?
Your error message does not come from your script, but from the called one.
It sounds like the droidbox.sh script is not very smart and requires you to set the current working directory before you can call it.
I would typically use also some more variables, so you better see what belongs together:
#!/bin/sh
set -e
BASEDIR="/home/xxx"
DROIDDIR="$BASEDIR/DrroidBox_4.1.1"
APKFILE="$BASEDIR/a.apk"
cd "$DROIDDIR"
"$DROIDDIR/droidbox.sh" "$APKFILE"
If you dont use set -e you better combine commands which need to succeed together:
cd "$DROIDDIR" && "$DROIDDIR/droidbox.sh" "$APKFILE"
Otherwise the cd might fail when the directory is missing and all following commands execute in the wrong directory.
This error is because you're running the script in a different folder than the folder that houses your "scripts/droidbox.py" file. You can fix this in the following way(s):
Move the "scripts/" folder to the directory that you're running this script out of using "mv /path/to/scripts/ ."
Move your customer script to the folder that contains scripts/droidbox.py and run the script from there

Why is my command not found but works in the command line?

This is a part of my script:
read -p "[q] What is the meteor project's location? "
PATH=$REPLY
cd $PATH
cd src
echo $(pwd)
mrt bundle bundle.tar.gz
It prints the correct working directory. Entering mrt bundle bundle.tar.gz in the console works.
Executing my script, I get meteor_bundle.sh: line 9: bash: command not found. What could be the reason?
You choose the variable name poorly: PATHhas a special meaning in Bash: it is used to indicate Bash where to look for executable programs. Choose another name (preferably lowercase, see #Gordon's comment) and it should be fine.
You should use the full path for mrt because you changed the value of the PATH. Or choose another name for the variable from the second line.
See the following post to understand exactly what is the purpose of the PATH environment variable:
http://www.linfo.org/path_env_var.html

Resources