LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2012, 06:32 AM   #1
dellafaille
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Rep: Reputation: Disabled
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"

Last edited by dellafaille; 06-16-2012 at 06:39 AM.
 
Old 06-16-2012, 07:29 AM   #2
dellafaille
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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"

Last edited by dellafaille; 06-16-2012 at 07:31 AM.
 
Old 06-16-2012, 07:53 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by dellafaille View Post
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 View Post
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/.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FC4 PHP5 & GD problem... pls help cosmicperl Fedora 1 12-09-2006 11:12 AM
Pls help with Programmers toolkit gFTP & jEdit & gVim stardotstar Linux - General 2 05-13-2006 07:11 AM
List of stuff on Suse 10.0 Eval CDs 4 & 5 [?] SUSE / openSUSE 2 01-23-2006 05:26 PM
pls pls pls help me ! i'm tired with httpd config on fedora apache 2.0.48 AngelOfTheDamn Fedora 0 01-24-2004 06:12 PM
xmms & pls speed tyccea Linux - Newbie 0 08-27-2003 03:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration