LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Getopts & eval help pls (https://www.linuxquestions.org/questions/linux-newbie-8/getopts-and-eval-help-pls-4175411754/)

dellafaille 06-16-2012 05:32 AM

Getopts & eval help pls
 
hi im trying to use getopts to learn the variation of ways to create a small script in linux however i get a syntax error when i try to run my script:
-line 25 syntax error: unexpected end of file
what do i need to put in or what am i doing wrong?

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="$opt3"


dellafaille 06-16-2012 06:29 AM

omg
 
waw my god im starting to get addicted to linux ... WTF?! xD
after so much cursing to this OS and hours i passed after its rly getting fun doing it

Okay i found the code by myself

BUT if you guys wanne try to solve it then you may not look down untill you solved it yourself :P
NO peeking!!!
Peeking == Cheating!! if [ peeking=cheating ] then echo "you cheater!"


However if you found an easier way to do it pls post below.
it's a start in linux in how to make a small script shell program.
quite usefull








































Congratsulations you just cheated ! :P
#!/bin/bash
opt1=""
opt2=""
opt3=""

while getopts "ab" opt;
do
case $opt in
a) opt1=" find / -type f -name *txt " ;;
b) opt2=" 2>/dev/null " ;;
esac
done

opt3="$opt1""$opt2"
eval "$opt3"

unSpawn 06-16-2012 06:53 AM

Quote:

Originally Posted by dellafaille (Post 4704718)
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 (Post 4704718)
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/.


All times are GMT -5. The time now is 08:24 AM.