if I pass a file as positional parameter, bash complain when I put it in a variable -


It is quite strange: I have a function that takes a file path, read the last line and return values.

Now, I would like to call this function with another script; So I just cross the path of the file, and get $ 1 in the function, so I can use it.

But it seems that for some reason, Bash does not like the idea of ​​specifying me $ 1 variable

  printval () {textfile = $ 1 Result = $ (tail N $ $ text file) resonance results $   

When do I call function :

  printval "/ user / admin / test.txt"   

Now, no matter how I pass the file path (I pass it a trough Can be either variable or string), and no matter if I use $ 1 in the function, or if I explicitly write the file, the result is that I "allow" Rejected "is saying error.

Strangely enough, if I function am

  Printvl () {result = $ (tail-N + 1 "/ user / admin / test.txt") echo The result $   

I have not found any errors, and the file has been read correctly.

What am I doing wrong? I just want to give a path to a file in the function, so I use the contents of that file in the function.

Is there a file name in the actual file you are using? That's it, are you actually typing something like this:

  printwall "/ user / which doe / test.txt"   

writing should defensive quote:

  Printvl () {textfile = $ 1 result = $ (tail n 1 "$ Tekstfail") "$ result echo"   < p> quote parameter not survive passing you  textfile  and  / user / j doe / test.txt  to  textfile = $ 1  is set You can, but when you use it, then Bash is expanding Ga  Taylor-N1 $ Tekstfail  and  tail-N1 / user / J doe / test.txt , which references the two file probably does not exist. You need to cite using the  $ textfile  ( tail -n 1 "$ textfile" ).   

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -