0

I am new to Shell command, can anyone help me about these two lines of Shell command.

filepath=$(cd "$(dirname "$0")"; pwd)
file_1=$(echo -e "${filepath}"|awk -F "$0" '{print $1}')

I have tried in my (KVM) VPS, but show like this:

[root@v97123 ~]# filepath=$(cd "$(dirname "$0")"; pwd)
dirname: invalid option -- 'b'
Try 'dirname --help' for more information.
tripleee
  • 175,061
  • 34
  • 275
  • 318
J. Lu
  • 1
  • 1
  • `$0` only makes sense inside a script. In an interactive shell, `$0` is typically `-bash` which looks like an option name to `dirname`. What are you actually hoping to accomplish? – tripleee Aug 23 '19 at 08:51
  • I just try to understand the code from Github. Everything is new to me. – J. Lu Aug 23 '19 at 08:55
  • `file_1=${0#"$filepath")` would be a more efficient and idiomatic way to get the basename of the script, or just `file_1=$(basename "$0")` if you don't mind calling an external utility. The Awk invocation will produce the wrong result if the script name contains whitespace (which is pretty unlikely, but still quite an unnecessary bug when you can obtain the correct result with a built-in). – tripleee Aug 23 '19 at 08:55
  • So your question isn't actually what the dollar signs do, but the purpose of this code? It extracts the name of the script into a variable. More commonly you'd see `file_1=${0##*/}` but the double `#` is not compatible to old `sh`. – tripleee Aug 23 '19 at 08:56
  • `awk -F "$0"` looks really weird though; not sure that's portable or whether I can correctly guess the intent. Can you link to the code on Github? – tripleee Aug 23 '19 at 09:02
  • [This?](https://github.com/ToyoDAdoubi/doubi/blob/master/brook-pf.sh) Horrible; don't try to learn from this. Paste it to http://shellcheck.net/ to get an idea of the amount of errors. – tripleee Aug 23 '19 at 09:04
  • [link](https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/ss-go.sh) – J. Lu Aug 23 '19 at 09:07
  • Yeah, looks like a fork of the same code. – tripleee Aug 23 '19 at 09:08
  • thanks a lot for your answers, I should try to learn somewhere else. – J. Lu Aug 23 '19 at 09:09

0 Answers0