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!

14
  • 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). Commented Nov 27, 2017 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 minimal reproducible example that shows how you're invoking it, what you expect it to do, and what it's actually doing instead. Commented Nov 27, 2017 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 -- best practice is [ -e "$1" ] && [ -d "$1" ]. Commented Nov 27, 2017 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 defines POSIX-defined utilities as using only all-caps names, and reserves lowercase names for applications -- such as user-written scripts). Commented Nov 27, 2017 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? Commented Nov 27, 2017 at 22:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.