3

Hi I had a question how to how write a script that would first check to see if a given filepath exists and if so outputs the path in readable sentences. I was going to start with

if [ -e "$1" -a -d "$1" ]; then
  echo $PATH 
else 
  echo "Path does not exist"
fi

so basically I'm just a little confuse on when I run my script ./myscript filename how exactly filename gets passed into $1 so I can check to see if it exists. I was also wondering how to do this to see if a user exists on the system. Any help is greatly appreciated!

  • One question to a question, please -- "how to see if a user exists on a system" should be its own distinct question (if it doesn't already exist in the knowledgebase, which would astonish me). – Charles Duffy Nov 27 '17 at 21:58
  • More immediately, though -- what part of your existing code *doesn't* work? (Do note that you're echo'ing `$PATH`, not `$1`, which is a quite different thing). Please provide a [mcve] that shows how you're invoking it, what you *expect* it to do, and what it's actually doing instead. – Charles Duffy Nov 27 '17 at 21:58
  • 1
    ...also, note that `-e` is a superset of `-d`, so `[ -d "$1" ]` will behave exactly the same. Also, note that `test -a` is marked obsolescent [in the POSIX standard](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) -- best practice is `[ -e "$1" ] && [ -d "$1" ]`. – Charles Duffy Nov 27 '17 at 21:58
  • (BTW, do be sure that your real code isn't using `PATH` as a name for a user-defined variable -- conflicts caused by that kind of situation are part of why [the relevant standard](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) defines POSIX-defined utilities as using only all-caps names, and reserves lowercase names for applications -- such as user-written scripts). – Charles Duffy Nov 27 '17 at 22:03
  • yeah "how to see if a user exist on a system" should be it's own question sorry about that. and I'm just conceptually having trouble understanding when I run this program ./filepath filename do I use "$1" to pass filename into my tests? so I could just use either -e or -d on its own? – Nathan Barnes Nov 27 '17 at 22:04
  • If you run `./yourscript filename`, then yes, `"$1"` will evaluate to the filename. – Charles Duffy Nov 27 '17 at 22:05
  • @CharlesDuffy I'm hoping if the filepath exists then to output the path in readable sentences which is why I just use $PATH on its own – Nathan Barnes Nov 27 '17 at 22:05
  • ...so, to be clear, if `./yourscript somedir` is called, and `somedir` exists (either as an absolute path or relative to the current working directory), you want your script's output to be something like `/bin:/usr/bin:/usr/local/bin`, otherwise you want your script to have no output? What use is that? – Charles Duffy Nov 27 '17 at 22:06
  • BTW, for a general discussion of how shell commands are processed (of which parameter expansion -- the step that replaces `$1` with a value -- is a component), see http://stuff.lhunath.com/parser.png. The most important think to grok here is that there are steps that go in order, and that the parser never goes backwards (from parameter expansion to reading/parsing) without code explicitly causing it to do so (which, generally, [one should never use](http://mywiki.wooledge.org/BashFAQ/048) without extremely compelling cause). – Charles Duffy Nov 27 '17 at 22:09
  • @CharlesDuffy Correct except for the second part I want it to output an error message if the path doesn't exist – Nathan Barnes Nov 27 '17 at 22:10
  • ...so add an `else` clause to your `if`. – Charles Duffy Nov 27 '17 at 22:11
  • @CharlesDuffy Thanks for the help and the further explanation! It's an assignment for one of my classes and I was in need of further clarification – Nathan Barnes Nov 27 '17 at 22:12
  • http://wiki.bash-hackers.org/syntax/pe is another reference on parameter expansion -- goes into more advanced content than what you need right now, but probably useful to keep around. – Charles Duffy Nov 27 '17 at 22:12
  • btw, see [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo) -- *always* quote your expansions; that means `echo "$PATH"`, not `echo $PATH`. If you had `IFS=:` run earlier in your script, for instance, then `echo $PATH` without the quotes would replace all the `:`s in your PATH with spaces, and expand each component created by that unquoted expansion as a glob, instead of representing the value accurately. – Charles Duffy Nov 27 '17 at 22:14

0 Answers0