LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-30-2014, 08:01 AM   #1
floriantraun
LQ Newbie
 
Registered: May 2014
Posts: 3

Rep: Reputation: Disabled
Problem with a Bash Script


Hi,

i've written a bash Script but I always get an error and I don't know why. The Script is supposed to recursivley Download a website an then parse all words out and put them in a word list.

Script:
Code:
#!/bin/bash
if [ "$1" -a "$2" ]
then
****echo ">> $(date +'%T') - Starting to downlad $1. This can take a long time..."
*
****#download websites recursively to ./temp/ directory, skip non-text files
****wget -r -l 2 --random-wait --user-agent='Mozilla/5.0' --quiet -R .jpg,.jpeg,.png,.gif,.bmp,.flv,.js,.avi,.wmv,.mp3,.zip,.css,.pdf,.iso,.tar.gz,.rar,.swf,.PNG,.GIF,.JPG,.JPEG,.BMP -P "./temp/" $1
*
****echo ">> $(date +'%T') - Finished downloading, creating wordlist..."
*
****#rescursively search for words that match out criteria in all files, 8+ chars, alpha
****page=`grep '' -R "./temp/" | sed -e :a -e 's/<[^>]*>//g;/</N;//ba' | tr " " "\n" | tr '[:upper:]' '[:lower:]' | sed -e '/[^a-zA-Z]/d' -e '/^.\{9,25\}$/!d' | sort -u`;
*
****echo "`date +"%T"` - Wordlist created!"
****echo ">> Fetched lines: $(echo "$page" | wc -l)"
*
****if [ -f '$2' ]
	then
********echo ">> File $2 already exists, merging files!"
********echo "$page" >> "$2";
********cat "$2" | sort -u -o "$2";
********echo ">> Wordlist merged with $2 and now has $(cat "$2" | wc -l) lines!";
****else
********echo "$page" > "$2"
********echo ">> Wordlist saved to $2!"
****fi
*
****#remove temporaray website directory
****rm -rf "./temp/"
else
****echo "Error: Parameter URL required!"
****echo "Example: $0 https://www.iana.org/domains/example/ ./wordlist.txt"
fi
Error:
Code:
./word_creator.sh: line 18: syntax error near unexpected token `then'
./word_creator.sh: line 18: `	then'
Thanks!
 
Old 05-30-2014, 08:29 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Look at what is different on line 18. You're missing all those asterisks. Add the four asterisks before the word 'then' on line 18 and see the problem goes away.
 
Old 05-30-2014, 08:42 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Am I missing something here? Asterisks are not valid to use as comments; that script will never work there should be errors starting at line 3. Replace all those asterisk series with single # characters if you want to comment out a line.

Or are those asterisks merely there to show indentation because cut/paste screwed it up or something? Indentation doesn't matter in a shell script by the way except for read-ability. Perhaps there are indentation requirements for some cases, but this level of simple script using if-else-fi, no way you don't "have" to indent, although I recommend you do. Either case, if you added those to illustrate simple indentation, don't do that.

Having said all that weibullguy is correct Line 18 is the one line that doesn't have this notation, so as a result it's different and potentially the cause for complaint. I just don't see how you could "get" there because as I say, a normal command parser would complain at line #3.

I grabbed the full text of your script, cut out all the asterisks and ran it, to a degree; for instance I'm not interesting in having those exact actions occur on my system, so I commented out some lines. Line 18 where your "then" statement is, has a TAB before the "then", that seems to not cause problems also with bash. For instance if I make a simple script:
Code:
#!/bin/bash
if [ 1 == 1 ]
 [TAB]  then
    echo "a";
else
    echo "b";
fi
exit 0;
This works. Same as if the indentation were by spaces, or no indentation at all. So indentation either by spaces or tabs doesn't affect the statement there.

Last edited by rtmistler; 05-30-2014 at 08:51 AM.
 
Old 05-30-2014, 09:38 AM   #4
floriantraun
LQ Newbie
 
Registered: May 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
The asterixes werd put in by the Forum Software. Not by me
 
Old 05-30-2014, 09:43 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Are you still getting the error? Try taking the TAB out of the start of line 18.

Add "set -xv" after line 1 of the script to enable script debug and see what's going on.

Also try instead double quotes around $2 in the if test in line 17 versus single quotes which you have now.

You should determine the count of arguments prior to attempting to use arguments. It may be complaining that nothing exists if you didn't give it a second argument. $# is the script variable to tell you the count of arguments passed to your script, therefore you can offer a usage statement after a simple test of $#.
 
Old 05-30-2014, 10:27 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by floriantraun View Post
The asterixes werd put in by the Forum Software. Not by me
This is not supposed to happen. Please check if you have some hidden (control) character in your script
Code:
sed -n 4p word_creator.sh | od -c
(where 4 is the number of the first line were asterisks appear, as in your post) or check the encoding of the script itself (UTF-8, UTF-16 and so on)
Code:
file -bi word_creator.sh
If there is nothing strange in the file itself you may consider to submit a feedback in the "LQ Suggestions & Feedback" forum to bring the attention of the administrator to the problem.
 
  


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
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 01:57 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 05:39 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:05 AM.

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
Open Source Consulting | Domain Registration