The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Include PERL script with in the unix shell script ganapati UNIX for Dummies Questions & Answers 1 04-29-2008 09:18 AM
help me in sending parameters from sqlplus script to unix shell script Hara Shell Programming and Scripting 2 01-29-2008 12:31 PM
check in unix shell script so that no one is able to run the script manually adi_bang76 Shell Programming and Scripting 1 11-16-2006 07:43 AM
Difference between writing Unix Shell script and AIX Shell Scripts haroonec AIX 0 04-11-2006 11:27 PM
How to run unix commands in a new shell inside a shell script? hkapil Shell Programming and Scripting 2 01-04-2006 03:56 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-18-2006
Registered User
 

Join Date: Jun 2006
Posts: 4
UNIX Shell Script Help

Hello,
Just a note I am SSH Secure Shell and a logic account and am currently doing basic UNIX Shell Scripts.

As part of an excercise I was told to "create an algorithm in pseudocode for the process of making a file executable. You would need to accept the filename as an argument, change the permissions on that file, output a message indicating the file is now executable, and do a long listing on that file to show the permissions"

My script consisted of the following -
#!/bin/bash
echo What is the name of the file you wish to make user executable
read filename
$bash chmod u+x $filename
echo The file $filename has now been made user executable
ls -la $filename

This worked fine, the next part of the excercise required me to - "add the following
A usage clause, to check the correct number of arguments are passed to the script
An if statement that checks that the file specified by the argument exists. If it doesn't exist, display a suitable message, otherwise, proceed with the chmod command. "


I changed my script to -
#!/bin/bash
echo What is the name of the file you wish to make user executable
read filename

# usage clause
if [ $# -ne 2 ]
then
echo "usage: $0 f1 f2" # follows typical manpage convention
# check file to move exists
elif [ ! -f $1 ]
then
echo "File $1 does not exist"

$bash chmod u+x $filename
echo The file $filename has now been made user executable
ls -la $filename

I then try to execute the script, it asks 'what is the name of the file you wish to make executable' and i type the name. However when i press enter it returns the error
line 17: syntax error: unexpected end of file

Can someone please help me fix this, I have been at this all day and just want to get this excercise done. Sorry about the long winded post just wanted to make sure all the information was there to make it easy for people to pinpoint the problem. Cheers for any help and input, Benny
Reply With Quote
Forum Sponsor
  #2  
Old 06-18-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
Code:
#!/bin/bash

if [ condition1 ]
then
   doThing1
elif [ condition2 ]
   do thing2
fi
Reply With Quote
  #3  
Old 06-18-2006
Registered User
 

Join Date: Jun 2006
Posts: 4
Hello,
thanks for the help with that I have now solved the problems with that script. However my final problem is with a script that i need to make to spell check a file and return the mispelt words and the line number of the mispelt words.

I have to implement -

myspell(file)
FOR each word in the mispelt list \\ use the spell command to create the list
line = find the line where the word occurs in the file \\ use grep with option n
DISPLAY the word
DISPLAY line
ENDFOR
END

My script currently looks like -
#!/bin/bash
echo What is the name of the file you wish to spell check?
read filename
word=`spell $filename` > mispelt.txt

line=`grep -n $word $filename` > mispelt.txt

echo $word
echo $line

exit

When I run it I get -
mary.txt
grep: can't open Marye
grep: can't open wamb
grep: can't open wittle
Delisious Marye wamb wittle
mary.txt:4: Delisious!
It finds the 4 words mispelt but will only display the line number for the word 'delisious' and can't get a line number for the other 3 words.
Reply With Quote
  #4  
Old 06-19-2006
Registered User
 

Join Date: Jul 2005
Location: England
Posts: 183
Your construct for searching for each word is incorrect.

Assuming you are using something like './myscript' to execute it then if you change the fisrt line from '#!/bin/bash' to '#!/bin/bash -x' and then run it you will cause the script to echo out each line it tries to execute before actually doing so. This is an invaluble debug tool for shell script writing.

Last edited by Unbeliever; 06-19-2006 at 12:13 AM.
Reply With Quote
  #5  
Old 06-19-2006
Registered User
 

Join Date: Jun 2006
Posts: 4
So i ran the script and it came back -
+ echo What is the name of the file you wish to spell 'check?'
What is the name of the file you wish to spell check?
+ read filename
mary.txt
++ spell mary.txt
+ word=Delisious
Marye
wamb
wittle
++ grep -n -i Delisious Marye wamb wittle
grep: can't open Marye
grep: can't open wamb
grep: can't open wittle
+ line=
+ echo Delisious Marye wamb wittle
Delisious Marye wamb wittle
+ echo

+ exit

So i gather its finding all the words, but its trying to execute them all at once, is my grep command -n -i right and do i need to split them up?
Reply With Quote
  #6  
Old 06-19-2006
Registered User
 

Join Date: Jul 2005
Location: England
Posts: 183
Now you can see the problem here:

++ grep -n -i Delisious Marye wamb wittle

Grep takes various options as argument (-n and -i in this case) then the next argument is the pattern to search for (Delisious) and the remainder is a list of file to search the pattern for.

So in this case you searched case independantly (-i) with line numbers (-n) for the pattern 'Delisious' in files called Marye, wamb and wittle.

You need to wrap the whole thing in some form of for loop. For example (not tested).

Code:
words=`spell $filename`

for word in $words
do
  line=`grep -n $word $filename | awk -F: '{print $1}'`
  echo "Mispelling of $word found on $line"
done
As an aside you have 2 lines in you original code:

word=`spell $filename` > mispelt.txt
line=`grep -n $word $filename` > mispelt.txt

In each case you are executing a command and catching the output in a variable and then redirecting output to a file. But of course all standard output has already been placed in the variable so the '> mispelt.txt' is redundant in both lines.
Reply With Quote
  #7  
Old 06-19-2006
Registered User
 

Join Date: Jun 2006
Posts: 4
ah oh, thankyou for that. I tried to implement a for loop at first but couldn't really understand how to make it work, now I do. Thankyou for helping a confused newbie out with the this stuff, cheers benny
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0