Passing variables to shell script
- You have to instruct you script that DIRECTORY is a variable, passed as first script argument.
- You have to enclose your variable into double-quotes in order to ensure spaces and special characters, like empty variables, to be correctly parsed.
Sample:
#!/bin/sh
DIRECTORY="$1"
if [ -d "$DIRECTORY" ]; then
echo "Directory '$DIRECTORY' exists"
else
echo "Directory '$DIRECTORY' does not exists"
fi
echo "$DIRECTORY"
Other sample:
#!/bin/bash
DIRECTORY="$1"
FILE="$2"
if [ -d "$DIRECTORY" ]; then
if [ -e "$DIRECTORY/$FILE" ]; then
printf 'File "%s" found in "%s":\n ' "$FILE" "$DIRECTORY"
/bin/ls -ld "$DIRECTORY/$FILE"
else
echo "Directory '$DIRECTORY' exists, but no '$FILE'!"
fi
else
echo "Directory '$DIRECTORY' does not exists!"
fi
echo "$DIRECTORY/$FILE"