cp command simply not copying - linux

I am working on a shell script and for some reason when I say
cp full_path/* full_path_directory/
I get an error. I have echoed out the command and when I run what it echos in an interactive shell it works. I can't figure out why it won't work in a shell script. I'm using full paths rather than absolute. I have tried to putting a slash at the end of the destination directory and then not putting a slash...what else could it be?
Error:
cp: /opt/local/apache2/htdocs/baseline/*: No such file or directory
So when I echo it out I get:
/opt/local/apache2/htdocs/baseline/* /opt/local/apache2/htdocs/test/

It means what it says. There are no files in /opt/local/apache2/htdocs/baseline/ directory, or you don't have permissions to read the directory. What does ls show you?

Related

How to execute from any directory Bash script that sources other Bash scripts (not using path variable)

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

shell script mv is throwing unhelpful error "No such file or directory" even though i see it

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.
Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.
The error I get is here:
ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved
When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.
If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.
I don't understand why I would get this error so, any help would be great.
Add a / after the path to specify you want to move the file, not rename the directory.
You should try this:
mv $PWD/\* /home/user/directory/
Are your variables properly quoted? You could try :
ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved
If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

shell script : appending directory path and filename

I want to copy a file from a directory using shell script
Suppose I save the directory and file name seperately as
dir=/home/user/directory/
file=file_1
to copy the file Im using this command in my script
cp $dir$file .
But I get this error
/bin/cp omitting directory '/home/user/directory'
I have tried all combination eg. omitted the trail backslah from variable dir, etc but nothings working. I cant understand what is wrong with this code. Pleas help
Maybe the command $dir$file is not getting unpacked in the shell (ie only the directory variable is getting unpacked, not the file variable)!!!!!
It looks like you are having problem with expansion in cp $dir$file . In order to prevent possible problems, it is better to protect your variable with braces and double quote the full path/file to make sure you don't get caught by spaces in either the filename or heaven forbid the user's dirname:
cp "${dir}${file}" .
This will prevent the possibility the second $ is missed. Also make sure you have read access to other users /home (if you are root or using sudo you should be fine)
If you see this, when you somehow assign an empty string to file somewhere. Search your script for file= and unset file.
You can also debug this by adding
echo ".${file}."
in the line before the cp command. I'm pretty sure it prints .., i.e. the variable is empty or doesn't exist.

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

Resources