bash script error: file or directory doesnt exist - bash

I'm using Ubuntu 14.04
This is my first time writing a bash script.
Here it is:
#!/bin/bash
${file}="/home/isra/files/mongoTemp.json"
${fdiff}="/home/isra/files/mTempDiff.json"
.
.
commands
.
.
mv ${file} ${fileold}
.
.
commands
.
.
exit
But it gives me these two errors:
./index.sh: line 4: =$HOME/files/mongoTemp.json: No such file or directory
./index.sh: line 5: =$HOME/files/mTempDiff.json: No such file or directory
mv: missing file operand
couldn't open [$HOME/files/]
Honestly, I'm not quiet sure what the problem is.. The files do exist, I've manually created them when it first complained about them but it didn't solve the problem and also, the mv command is not working. What am I doing wrong?

When assigning to variables, use foo=, and not $foo=, or ${foo}=
I.e., the lines:
${file}="/home/isra/files/mongoTemp.json"
${fdiff}="/home/isra/files/mTempDiff.json"
Should be:
file="/home/isra/files/mongoTemp.json"
fdiff="/home/isra/files/mTempDiff.json"
The rest looks fine.
Note that the two first errors are because of the ${..}=... lines.
The variables $file and $fdiff are empty, which then cause the mv error "mv: missing file operand". Some later command you haven't shown causes the final error. Though, most likely, everything will be fixed by properly assigning the variables.

Related

Bash keeps throwing "syntax error: unexpected end of file"

I have been trying to write a shell script to copy and rename files, and I am running Ubuntu 20.04 on Windows subsystem for Linux. I'm currently just at the "copy" part, and I have the following, with <path> standing in for my file path.
#!/bin/bash
for file in "<sourcepath>"/*; do echo "$file" && cp "$file" "<destinationpath>"
done
cd "<destinationpath>"
The script appeared to execute correctly until I added the cd "<destinationpath>" line. Now the console says "syntax error: unexpected end of file". I don't have any newlines or anything I can think of that would be causing this.
The last two lines of hexdump -c:
I used dos2unix on the file, and I was able to run it without errors.

How to make a shell script take an argument passed in the command line?

I have a shell script (count_reviews.sh) that contains the following:
#!/bin/bash
grep -c "Author" "#1"
I have tried using "$1" as I found that this takes the first argument in the command line but I still get the error mentioned below. I have also used chmod +x to make my file executable.
The script counts the number of times "Author" appears in the file. I am required to be able to make the command line take an input of "% ./count_reviews.sh hotel_72572.dat" where hotel_72572.dat is an example file name. The number of times author appears will then be printed out underneath. When I do this however, I am getting an error -bash: fg: %: no such job. What is causing this and how do I fix it? My count_reviews.sh file is in the same directory as all of my hotel data files if that matters.
You should indeed use $1, but when you try to run your script just use ./your_script.sh your_argument don't add the % in the beginning.

open directory with a variable bash shell

I'am reading in a variable that will contain a version of a certain file (Ex.: V1.0.10) by the following command.
read Version
and there is a possibility that that variable contains dots and I remove them by the next command:
New_Version=`echo $Version | sed -e 's/\.//g'`
but if I use this variable later on in the script, nothing changes at this variable, and I just use the cd command:
cd /data/group/$New_Version
or
cd /data/group/"$New_Version"
Then the error: No such file or directory... : line ...: cd:/data/group/V1010.
I double checked, the files exists, the name is correct but he doesn't find or recognize the directory?
What am I doing wrong?
Hope someone can help!
Thanks
UPDATE: The OP says that the problem was a hidden character in his input. This answer does not describe how to solve that problem. Nonetheless, the OP has marked this answer as accepted. See the comments of Charles Duffy for the actual solution to the OP's problem.
Caveat: I am taking everything in your problem description literally, which leads to the answer below. If you provide examples of the strings that will be passed through $Version it would help clarify the issue.
As I understand it you're reading in the full path of a file in your variable with read Version. Now if you say echo $Version you should get /path/to/foo.bar.
I don't think you'd want to cd into the file /path/to/foo.bar. You'll get an error: Not a directory, because it's a file, not a directory.
Now, consider what sed -e /\.//g will do to the pathname.
echo "/path/to/foo.bar" | sed -e '/\.//g'
/path/to/foobar
Does /path/to/foobar actually exist? No, because foo.bar was a file. You'll get an error: No such file or directory, because the foobar directory does not exist.
If I understand what you are trying to do, you are trying to extract the directory that contains the file specified by $Version. The command dirname /path/to/foo.bar will return /path/to. So you want to set New_version=$( dirname "$Version" ), at which point you should be able to cd $New_version.
P.S. Make sure $Version is reading in an absolute path name, not a relative name, so that it's independent of where you run the script from.

Compressing using tar with bash script : Line n is a directory

I'm new with bash script and now trying to make a back-up file using it. What I'm facing is below script (line 47 in my bash file),
$(tar -czvPf ${folder_backup}/rajal/backup.tgz -C /var/www/tmk/app .)
always gimme error on the shell ./test: line 47: ./: Is a directory, while it works on Terminal, nicely.
FYI, folder app is folder with files and subfolders.
Could someone help me out? Thank you for any help.
$(command)
means: execute command and return its output. To illustrate, it is very often used like this:
result=$(foo 4 123)
After this is evaluated, result will hold whatever the command foo 4 123 output.
If you use that construct directly - not as an argument to another command, or in a variable assignment, the shell will try to execute the output of the command. While this is sometimes wanted, it often is not, and that's what you're seeing.
So just remove the $( ) from your command and run tar directly. If you want to capture the output of tar, either redirect it to a file or use the construct above.
Do note that $(command) and ${env_var_name} are completely different. Syntax matters.
Like #Mat Why do you have $( ) around the tar?, so i just removed it and it is working now :D

Issue with setting $PATH directories

For some strange reason, I'm getting a "No such file or directory" error for my $PATH variable. I have tried to edit my path using export, changing it from what it was originally to every permutation from a single directory path to the original.
When there is one directory (e.g., export PATH=/bin), I get "/bin: Is a Directory". But once I add more than one directory (e.g., export PATH=/bin:/sbin), I get "No such file or directory".
I'm curious to see what the cause of this issue is!
RE; your comment:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/u‌sr/local/mysql/bin: No such file or directory will be generated if you have a line which says:
$PATH
maybe on its own, or maybe you have $PATH=.... That is, the shell is trying to execute a program named:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/u‌sr/local/mysql/bin
Lose the $ on the left-hand side.
I'm not sure you are using the export variant. You almost certainly have spaces in there and you shouldn't, as per the following transcript:
pax> PATH= /bin
bash: /bin: is a directory
pax> PATH= /bin/sbin
bash: /bin/sbin: No such file or directory
The first is caused because you're setting the path temporarily to an empty string while attempting to run that directory. That's because you can do things like:
pax> xyzzy=1
pax> echo $xyzzy
1
pax> xyzzy=2 bash -c 'echo $xyzzy'
2
pax> echo $xyzzy
1
In other words, it's a way of changing an environment variable for a single command, and having it automatically revert when the command is finished.
The second case is simply because there is no /bin/sbin directory. So it detects that before it complains about the fact that you're trying to run a directory.
Setting a variable in bash is a no-space thing (unless you have spaces in your directory names, in which case they should be quoted). In addition, they need to be colon-sparated. Hence you're looking for things like:
PATH=/bin
PATH=/bin:/sbin
PATH="/bin:/sbin:/directory with spaces in it:$HOME/bin"
The export function will only change the variable for the current terminal session.
Write your PATH inside ~/.bash_profile if you want to change it permanently.
For this modification to work you have to close your current terminal and reopen it.

Resources