Quote:
Originally Posted by dellafaille
learn
|
Ahhh:
Code:
function howto() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls"; }
Quote:
Originally Posted by dellafaille
i get a syntax error when i try to run my script:
-line 25 syntax error: unexpected end of file
|
Either making the second line of your script read "set -vx" (w/o quotes) or executing your script as '/bin/bash -vx /path/to/script' will show you how it's interpreted and where it halts. Best way to start debugging shell scripts. (And of course you only ever edit and run scripts as
unprivileged user, right?)
Here's just correcting general wrongness and b0rkage:
Code:
#!/bin/bash
opt1="false"
opt2="false"
while getopts ab opt;
do
case $opt in
a) opt1="true" ;;
b) opt2="true" ;;
esac
done
if [ "$opt1" = "true" ]; then $opt1="find / *txt "; fi
if [ "$opt2" = "true" ]; then $opt2=" 2>/dev/null "; fi
opt3="${opt1}${opt2}"
eval echo "${opt3}"
exit 0
...but I think you want something more like:
Code:
#!/bin/bash
# Comment out when done debugging:
set -vx
# Just see this as a hint, it's superfluous:
unset OPTION MYPATH MYEXT MYARGS MYCMDLINE
# Consider this as Something Good:
LANG=C; LC_ALL=C; export LANG LC_ALL
# Functions are nice to reuse:
myhelp() { echo "scriptname: -p(ath), -e(xtension), -r(edirect errors)."; exit 1; }
# What you mean no args?
[ $# -eq 0 ] && myhelp
# Use UPPERCASE VARIABLE NAMES make them stand out, helps readability.
# Also think about script logic:
while getopts p:e:rh OPTION; do
# Proper quoting helps avoid simple errors, also see IFS:
case "${OPTION}" in
p) [ -d "${OPTARG}" ] && MYPATH="${OPTARG}" || myhelp;;
e) MYEXT="${OPTARG}";;
r) MYARGS="2>/dev/null";;
h|*) myhelp;;
esac
done
# Build the variable from what we have:
MYCMDLINE="find ${MYPATH:=~} -type f -iname ${MYEXT:=\*.txt} ${MYARGS:=2>&1}";;
# You don't need a string match:
[ ${#MYCMDLINE} -eq 0 ] && myhelp || eval echo "${MYCMDLINE}"
# A script may error out but you should always exit it the right way:
exit 0
* I'm just writing this w/o any testing so YMMV(VM) and wrt eval see
http://www.linuxquestions.org/questi...ommand-948751/.