I'm having an error while trying to execute a sh in my crontab (or in the normal shell) .
I made an user Xuser, and in his home directory make a symbolic link to /opt/app
/opt/app with this --> drwxrwxr-- Xuser test
In this folder I save serveral sh and jar.
Now in the crontab of this user or even in the shell if I tried to execute this:
./opt/app/bin/ind.sh
-bash: ./opt/app/bin/ind.sh: No such file or directory
The Sh file is
#!/bin/sh
export JAVA_HOME=/usr/java/latest/
export PATH=$PATH:$JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
java -jar /opt/app/bin/ind.jar
If I put a space between . / it works
. /opt/app/bin/ind.sh
: command not found
Error: Unable to access jarfile /opt/app/bin/ind.jar
( Access the SH but seems that the problem pass to the jar hahaha )
Someone notice the problem??
Thanks !!!
There is a big difference, when you put a space between the . and the / the . is no more part of the file path but is an alias of source and means "execute the content of this file in the current shell".
// execute in a subshell
./foo/bar/baz.sh
// execute in this shell
. ./foo/bar/baz.sh
// this is not the same file as previously, unless we are at the root (/)
. /foo/bar/baz.sh
Related
I am not trying to execute a Bash script from any directory by adding the script to my Path variable.
I want to be able to execute the script from any directory using the directory path to that file ... but the file I want to execute sources other files, that is the problem.
If I am in directory file with two scripts myFunctions.sh and sourceFunctions.sh
sourceFunctions.sh
#!/bin/bash
source ./myFunctions.sh
echoFoo
myFunctions.sh
function echoFoo()
{
echo "foo"
}
I can run myFunctions.sh and foo will print to console, but If I go up a directory and run myFunctions.sh I get error
cd ..
file/sourceFunctions.sh
-bash: doFoo.sh: command not found
Unless I changed source file/myFunctions.sh to source file/myFunctions.sh in sourceFunctions.sh.
So how can I source independent of my working directory so I can run sourceFunctions.sh from any working directory I want?
Thanks
You have the right idea. Doesn't need to be that complicated though:
source `dirname $0`/myFunctions.sh
I often compute "HERE" at the top of my script:
HERE=`dirname $0`
and then use it as needed in my script:
source $HERE/myFunctions.sh
One thing to be careful about is that $HERE will often be a relative path. In fact, it will be whatever path you actually used to run the script, or "." if you provided no path. So if you "cd" within your script, $HERE will no longer be valid. If this is a problem, there's a way (can't think of it off hand) to make sure $HERE is always an absolute path.
I ended up just using a variable of the directory path to the script itself for the source directory
so
#!/bin/bash
source ./myFunctions.sh
echoFoo
becomes
#!/bin/bash
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
source ${SCRIPTPATH}/myFunctions.sh
echoFoo
source
i have shell script to FTP a file from one server to another server called abc.sh below is the code inside it
#!/bin/bash
HOST='10.18.11.168'
USER='india'
PASS='India#2017'
FILE='inload.dat'
DIRECTORY='/inloading'
ftp -n $HOST <<END_SCRIPT
user $USER $PASS
cd $DIRECTORY
put $FILE
quit
END_SCRIPT
exit 0
i am able to run it using ./abc.sh and file also gets copied to remote server.
But when i use in crontab it is not ftp the file
below is the crontab entry
15 01 * * * /user/loader/abc.sh > /user/loader/error.log 2>&1
in the error.log it shows as local: inload.dat: No such file or directory
You're referencing the file inload.dat, which is relative to the directory the script is run from. When you run the script as ./abc.sh it looks for an inload.dat in the same directory.
Cron chooses which directory to run your script from when it executes (IIRC it generally defaults to /root or your HOME directory, but the specific location doesn't matter), and it's not necesarily the same directory that you're in when you run ./abc.sh.
The right solution is to make FILE and absolute path to the full location of inload.dat, so that your script no longer depends on being run from a certain directory in order to succeed.
There are other options, such as dynamically determining the directory the script lives in, but simply using absolute paths is generally the better choice.
Your local path is probably not what you want it to be. Before executing the ftp command, add a cd to the directory of where the file is located. Or have the full path name to the file $FILE.
I need to run a script in a remote machine from my JAVA code using runCommand() method. Now I can't always know the full path of the script as a particular directory name keeps changing. For example the path looks like this : /a/b/xxxxx/script . xxxx is the directory name that keeps changing and its the only single directory under /a/b/. Is there any shell command using which I can get the directory name ? I know using JAVA,but I specifically need shell command.
If there is only a single self-directory, another fool-proof way of doing it would be
cd */.
*/. is that this expands to the "self directory" (named .) in any subdirectory, which is of course the sub-directory itself. Refer the below example of how it works.
E.g.
$ pwd
/home/dude/
$ mkdir -p a/b/ldsnds/c
$ cd a/b/*/.
$ pwd
/home/dude/a/b/ldsnds
$ cd -
/home/dude/
$ cd a/b/*/./c
$ pwd
/home/dude/a/b/ldsnds/c
Below should give you the name of the directory in the directory "b".
$ find /a/b -type d -maxdepth 1 2> /dev/null
If you are so sure that it would always be one directory in /a/b then just store the output of find in a variable and move ahead.
Note: 2> /dev/null is just to get rid of errorneous warnings.
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.
Request to need a help or information of the two operators . and `` in linux
e.g.
$ cp /home/uddi/root/hello `pwd`
and
$ cp /home/uddi/root/hello .
Please suggest me
A small difference occurs after mkdir /tmp/lost; cd /tmp/lost; rmdir /tmp/lost.
After these stupid commands pwd will be a filename (/tmp/lost) and the current dir . does not exist.
I think you want an error when you try to copy a file in the "current" dir, so I would prefer the .. It will also avoid an extra command.
When you enclose something between back-ticks, the shell will run the contents and use the output from that/those command/s as an argument for the main command being run. In your example, the shell will run the pwd command and use its output as the 2nd argument to the cp call.
In your second example, the . character is a link to the current directory. The reason that both do the same thing is that . links to the current directory and pwd will print out the current working directory, which are the same. In this case, you are using two methods to expand to the same path.
EDIT:
You can see somewhat how . works by running ls -a in any directory. It will show you the . and .. directories, which are filesystem-level links to the current and parent directory, respectively.