0

So the script and file I want to access are both in the same directly (root). I can do

cat flist

And it prints it out fine. But if I call "cat flist" in a script(or any other file for that matter) it won't work. They are both in root. Any ideas how to fix this?

enter image description here

Also,

grep -w "asd" doc.data
sort -k2,2n doc.data
head -5 doc.data

This prints 3 different reports. I want it to first, get everything with the word asd, then sort, the print the top 5. How would I change it to accomplish this?

l'L'l
  • 44,951
  • 10
  • 95
  • 146

1 Answers1

1

The user the script is running as probably does not have the root directory (i.e., '/') as part of its $PATH. Which means that, in order to refer to a file in the root directory, you must give it the absolute pathname. In the case of the root dir, this just means prepending the slash, so that 'scriptname' just becomes '/scriptname'

It's worth noting that the default setup for most Linux accounts does not include the current directory in the path, either, the way Windows does. This means that an absolute path must be used to execute a script from another script -- even if they're both in the same place. (This is considered a Good Thing for reasons of security, or so I am given to understand.)

If I'm misunderstanding the question I apologize, but I hope this helps.

EDIT: As for the second part, combining commands in the way you want, well, this is one of my favorite things about the command shell, and it illustrates one of the reasons that the Linux-based shells are often considered more full-featured than what's usually available on Windows. You can use what are called pipes to send the output of one command into the input of the another. To do this, you combine the commands with the pipe, or vertical bar, character - '|' In this case, try:

grep -w "asd" doc.data | sort -k2,2n | head -5

The commands sort and head are both pipe-compatible (as are most native commands where it makes sense). This means that they use "standard input" (STDIN) and "standard output" (STDOUT). STDOUT is, by default, printed to the screen. But that's what the pipe character does - it sends STDOUT instead to the next command's STDIN. Hope this makes sense, and is useful. Give it a shot!

Jon Carter
  • 2,836
  • 1
  • 20
  • 26
  • Ah yeah, thanks fixed it. Any help for the second part of my problem?(not printing 3 different things) – Brian Jones May 26 '16 at 01:50
  • About my Linux vs. Windows remarks, I should point out that, while what I said holds true for the most common Windows shell, cmd.exe, there is another one called Powershell that seems to implement a sort of pipeline similar to this, but it seems harder to work with, at least in my experience. (It operates on input and output as data structures, instead of just text, I think). – Jon Carter May 27 '16 at 23:13