UNIX Shell Script Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX Shell Script Help
# 1  
Old 06-18-2006
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
# 2  
Old 06-18-2006
Code:
#!/bin/bash

if [ condition1 ]
then
   doThing1
elif [ condition2 ]
   do thing2
fi

# 3  
Old 06-19-2006
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.
# 4  
Old 06-19-2006
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 04:13 AM..
# 5  
Old 06-19-2006
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?
# 6  
Old 06-19-2006
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.
# 7  
Old 06-19-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass Oracle sql script as argument to UNIX shell script?

Hi all, $ echo $SHELL /bin/bash Requirement - How to pass oracle sql script as argument to unix shell script? $ ./output.sh users.sql Below are the shell scripts and the oracle sql file in the same folder. Shell Script $ cat output.sh #!/bin/bash .... (7 Replies)
Discussion started by: a1_win
7 Replies

2. Shell Programming and Scripting

Batch script to execute shell script in UNIX server

Hi team, My requirement is to transfer pdf files from windows machine to unix server and then from that unix server we should sftp to another server. I have completed the first part i.e From windows to using to unix server with the help of psftp.exe code: psftp user@host -pw password <... (1 Reply)
Discussion started by: bhupeshchavan
1 Replies

3. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

4. Shell Programming and Scripting

Dos batch script to execute unix shell script

Can anyone help me with a dos batch script to execute a shell script residing in an unix server. I am not able to use ssh. Thanks in advance (2 Replies)
Discussion started by: Shri123
2 Replies

5. Shell Programming and Scripting

FTP from windows to unix server using unix shell script

Hi, Is it possible to ftp a huge zip file from windows to unix server using unix shell scripting? If so what command i need to use. thanks in advance. (1 Reply)
Discussion started by: Shri123
1 Replies

6. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

7. Shell Programming and Scripting

help me in sending parameters from sqlplus script to unix shell script

Can anybody help me out in sending parameters from sql*plus script to unix shell script without using flat files.. Initially in a shell script i will call sql*plus and after getting some value from some tables, i want that variable value in unix shell script. How can i do this? Please tell me... (2 Replies)
Discussion started by: Hara
2 Replies

8. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies

9. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question