Launch Bash script with command line and full path - linux

I launch bash scripts normally with the ./ command.
But if I try to launch the script with the full path I get an error
No such file or directory
I am so confused, I made a search to be sure to get the right path.
$ pwd
/home/pi/server/
$ ls
start_scan
$ sudo chmod 777 start_scan
$ sudo find / -xdev -name start_scan
/home/pi/server/start_scan
$ ./home/pi/server/start_scan
-bash: ./home/pi/server/start_scan: No such file or directory
Do you have any idea what could the problem be? I am using a macbook to use SSH and connect to a Rapsberry Pi under Raspbian and execute the script there.

./ is no command, but a path that means the current working directory.
Your line is almost correct, just remove the dot at the beginning:
/home/pi/server/start_scan
When you type any path starting with a dot, the shell expands it to the current working directory, effectively searching in
/home/pi/server/home/pi/server/start_scan
which is obviously wrong.

Related

command l to access downloads directory in raspian

lines I have tried:
cd ~/downloads
cd /home/pi/downloads
cd ~/user/home/downloads
ls ~/downloads
cd ~./downloads
If I use the nano command the screen will come up but if Try to change directory it says "no such file or directory". Don't laugh its probably simple but I'm new to Linux and trying to learn command lines.

Move script.sh to bin

I'm new to shell programming and I'm trying to create a simple script that gives me some infos on the status of the machine (i.e date, time, users logged in etc) on Scientific Linux 6 (I know it's old, but the department of my university runs on it so there's no escaping)
Basically I've created my script "sysinfo.sh"
#!/bin/sh
....
exit 0
as root user I want to move it so that I can be able to execute it anywhere and I thought the right way to do it was
sudo mv sysinfo.sh usr/local/bin
but I get the error message
mv: cannot move `sysinfo.sh' to `usr/local/bin': No such file or directory
then I looked for the PATH and it gives me
$ echo $PATH
/u/geo2/sw//System/tools/bin:/usr/bin:/bin
What is the right place to move my script?
Best practice for these kind of manipulation or learning is to have scripts in your $HOME/bin directory.
mkdir $HOME/bin
export PATH=$PATH:$HOME/bin
mv sysinfo.sh $HOME/bin
chmod +x $HOME/bin/sysinfo.sh
If you anyway want to move it to /usr/local/bin, why not do that with:
sudo mv sysinfo.sh /usr/local/bin
chmod +x /usr/local/bin/sysinfo.sh
chmod command will make the script executable.
from chmod man:
x -- The execute/search bits.
The command that you posted indicates that you were trying to use the absolute path for copying, but you missed a leading slash --
the directory should be /usr instead of usr.
Try
sudo mv sysinfo.sh /usr/local/bin
Note that unless an absolute path is specified, the shell looks for the path relative to the current working directory.
In this case, the shell was looking for the subdirectory usr under the current directory which was not found;
hence the error message.
Thank you very much!
In the end, I didn't realize that the directory /usr/local/bin wasn't in the PATH
So i just needed to
export PATH=$PATH:/usr/local/bin
sudo mv sysinfo.sh /usr/local/bin
:D

mkdir $HOME/ error

I tried a one line script that worked in one Linux based distro(Linux mint) but doesn't work in another(Fedora). I typed the following line in my bash script.
mkdir $HOME/folder123
The error i receive:
bash: create.sh: No such file or directory
I tried creating a folder myself, it gave me a permission denied?
To clear things hopefully: mkdir is in the script create.sh and i run it in the terminal with the command bash create.sh
If I interpret your post correctly, you have a file create.sh that contains mkdir $HOME/folder123, and you're trying to run it by typing create.sh.
To execute a script, chmod +x yourscript.sh then run it with either ./yourscript.sh if it's in the current directory, or /home/whatever/yourdir/yourscript.sh if it's in another directory.
To make just yourscript.sh work, you have to place it in a directory listen in $PATH.
You can do this by copying it to any of the directories listed in echo $PATH.
Alternatively, you can create a new directory such as mkdir /home/you/bin and then add export PATH="$PATH:/home/you/bin" at the end of your ~/.bashrc. Also make sure ~/.bash_profile contains the line source .bashrc. Then log out and in again.

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

Cannot execute bash script in redhat linux

I write a simple shell script to clean log files in redhat:
Filename: clean.sh
#!/bin/bash
rm -f *.log core.*
But when I typed clean or clean.sh, it always prompt
-bash: clean: command not found
-bash: clean.sh: command not found
What's the problem?
You probably don't have . (the current directory) in your $PATH (and that's a good thing; having . in your $PATH can be dangerous.)
Try this:
./clean.sh
And if the script file's name is clean.sh you can't run it as just clean, with or without a directory. The file name is clean.sh, and that's how you need to execute it.
Or you can change the name from clean.sh to just clean. Unix-like systems (that includes Linux) don't depend on file extensions the way Windows does.
problem 1: maybe the execute permission on clean.sh is not set. Do this:
chmod +x ./clean.sh
problem 2: RH Linux does not include CWD on the path by default. So, when you are in the same directory as clean.sh, type:
./clean.sh
That should execute it.

Resources