0

My script worked before changing it's directory (it was in the same directory as Main Dir:

#!/bin/bash

name=$1
amount=$2
cat "Main Dir/$name/sent.txt"|sort -k2|cut -d"  " -d"   "-f1|tail   -n-$amount

Now I've created aside "Main Dir" another directory "scripts" and put my script there.

#!/bin/bash

name=$1
amount=$2
cat "../Main Dir/$name/sent.txt"|sort -k2|cut -d"   " -d"   " -f1|tail -n-$amount

Trying to run it now from the command stroke by "sh path/name.txt param1 param2" but it say's "No such file or directory"?

How should I change the path in cat command ?

Ilya.K.
  • 291
  • 1
  • 13
  • Which folder do you run the script from? – jayant Nov 03 '15 at 21:04
  • I run it from scripts: sh /scripts/ – Ilya.K. Nov 03 '15 at 21:05
  • try to escape `Main\ Dir`. – sobolevn Nov 03 '15 at 21:05
  • https://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in. The answers there are not good (especially the highest voted one, I'd say `cd`ing in a script is very bad idea), but at least it gives you some idea. – 4ae1e1 Nov 03 '15 at 21:05
  • Bottom line: using relative paths in a script is bad. – 4ae1e1 Nov 03 '15 at 21:06
  • What do you suggest in that case as an alternative? Because I do want to run the script in such order of directories. – Ilya.K. Nov 03 '15 at 21:06
  • Depends. If you know the absolute path to the file, use the absolute path. If the path of the file is always relative to the script, then first determine the absolute directory of the script (linked question), then use the absolute path `"$scriptpath$relpath"`. – 4ae1e1 Nov 03 '15 at 21:08
  • The absolute path, you mean "~/mtm/...." in such manner of it? Because I tried it eather and cat still doesn't find it. In other words, the full path from "home"? – Ilya.K. Nov 03 '15 at 21:10
  • You don't know what "absolute path" is? Google. – 4ae1e1 Nov 03 '15 at 21:18
  • It is a nightmare to google something in programming. There are thousands of a long exhausting not relevant answers. Can you sum it by a few words instead please ? I tried to access my file in different ways, and it says that cat doesn't find it. Didn't though that a directory change makes such difference. – Ilya.K. Nov 03 '15 at 21:32
  • Google search for 'absolute path name' turns up lots of relevant information; searching for just 'absolute path' turns up even better information, if anything. I fail to see your nightmare with Google. (I am searching on `http://www.google.com/` and not a specific country site. It may be educated by my past searches to tell me about programming stuff over anything else. But the Wikipedia links appear on the first page in both searches.) – Jonathan Leffler Nov 03 '15 at 23:24

2 Answers2

0

Hm ... it works in a dependence of from which place I run the script. If I'm in the directory that includes Main Dir and scripts and I run it as I wrote in the question, but without the "..", it works. If I cd scripts and try to run it just by sh , then it doesn't find Main Dir. Can somebody explain this?

Ilya.K.
  • 291
  • 1
  • 13
0

The problem is that .. is relative to the working directory of the script, which is not generally the directory containing the script. The working directory is inherited from whatever process ran the script - e.g. your command shell.

It's usually safe to rely on $0 to be the path to the script, as the shell sets it that way and other programs usually do too. However, $0 may be a relative path. That is, if you go into the scripts directory and run ./myscript, then $0 will be ./myscript. But if you put the /home/ilya/project/scripts in your path and just run myscript, then $0 will be the full path /home/ilya/project/scripts/myscript.

We can strip the /myscript (or whatever the script name is) off the end of $0 by saying ${0%/*}.

Thus:

#!/bin/bash

name=$1
amount=$2
file="${0%/*}/../Main Dir/$name/sent.txt"
cat "$file" |sort -k2|cut -d"   " -d"   " -f1|tail -n-$amount
rob mayoff
  • 375,296
  • 67
  • 796
  • 848