LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2006, 03:51 AM   #1
kb100
Member
 
Registered: Aug 2006
Posts: 43

Rep: Reputation: 15
Scripting Help PLEASEEEEEEE!!!


Hi there, this is my code which i have done. Im a novice so im sure there are plenty of reasons why it doesnt work. The name of my file is

Code:
rename.sh
Please help

thanks


Code:
#!/bin/sh
clear
echo "Enter filename"
read filename
if grep $filename rename.sh
then
echo "Filename exists"
else
clear
fi

echo "Enter the new name for the second file"
read filename2
if grep $filename2 rename.sh
then
echo "Filename exists"
echo "Would you like to overwrite this file? y/n?"
read verdict
if[$verdict -eq"y"]
echo filename >> filename2
else
if[verdict -eq"n"]
echo "You have chosen not to overwrite the file. Goodbye."
exit
fi
fi
 
Old 08-26-2006, 04:29 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by kb100
Hi there, this is my code which i have done. Im a novice so im sure there are plenty of reasons why it doesnt work. The name of my file is

Code:
rename.sh
Is rename.sh the script file that you post ?
Quote:

Please help

thanks


Code:
#!/bin/sh
clear
echo "Enter filename"
read filename
if grep $filename rename.sh
then
echo "Filename exists"
Should be:
Code:
echo "rename.sh contains the string $filename"
This test make no sense if rename.sh is the currently executing script.
Quote:
Code:
...
echo "Would you like to overwrite this file? y/n?"
read verdict
if[$verdict -eq"y"]
Should be:
Code:
if [ "$verdict" = "y" ]
Quote:
Code:
echo filename >> filename2
else
if[verdict -eq"n"]
Same as the above.
Quote:
Code:
echo "You have chosen not to overwrite the file. Goodbye."
...
 
Old 08-26-2006, 04:41 AM   #3
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
scripting

Hi there. Thanks for reply. What i want to do in that script is write a script called rename.sh that will take two arguments. first argument is name of orginial file and second is name for new file. Am i doing the grep function right?
 
Old 08-26-2006, 04:43 AM   #4
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
scripting

Hi there. Thanks for reply. What i want to do in that script is write a script called rename.sh that will take two arguments. first argument is name of orginial file and second is name for new file. Am i doing the grep function right? am i searching for the existing file in the right place?


thanks again
 
Old 08-26-2006, 04:51 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The script you posted doesn't make use of its potential arguments, which are $1 and $2.

It is still unclear what you intend to do as your explanations are confusing.

What are the original, the new and the existing files ?

What are they supposed to contain ?
 
Old 08-26-2006, 05:00 AM   #6
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Well, this is what i think i have to do. Please tell me if i am right or wrong please. You people must be so annoyed with me but i have been through so many tutorials and unless i see examples it seems to be going right over my head!! This is what i must do

Code:
write a script called "rename.sh" that will take two arguments: the first is name of original file, and the second argument is new name for file. If use does not provide two arguments, a exit message should appear. the script should check to see if new name exists. if name does already exist it should prompt user to overwrite the file.
This is what i think i have to do. Please tell me if i am wrong.

Code:
Ask user for a file name that already exists (read filename) 
· Do an IF statement, if file doesn’t exist display an error message (echo “error, file doesn’t exist”) else continue 
· Ask user for another filename (read filename2) 
· Do another IF statement, if second filename doesn’t exist, create it and copy the contents of first file into it (something like echo filename >> filename2) 
· If second filename already exists, ask user if they want to overwrite it (echo want to overwrite (y/n)? Read verdict) 
· If [$verdict –eq “y”] then overwrite file same as before (echo filename >> filename2). 
· If [$verdict –eq “n”] then display a message using echo and exit.
 
Old 08-26-2006, 05:06 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Well, you are definitely missing the initial part, which is about as script picking two arguments. Your script doesn't make use of its arguments, as I already stated.

You should go back to your shell tutorial and look about what arguments are.
I gave you a hint with $1 and $2.
 
Old 08-26-2006, 05:12 AM   #8
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
i know what arguments are but i dont. i understand that the first arguments is $1 .... and so on $n but i dont understand
what an argument exactly does. What must a user input? Please give me an example it would make it so much easier. I have read
about 5 or 6 tutorials over 2 days. I know what an argument is but i dont understand what my script must do.
 
Old 08-26-2006, 05:43 AM   #9
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Could you explain to me the code you gave me before then? step by step?

Code:
1 #!/bin/sh
2 set -e
3 echo "The amount of commandline args is $#"
What i dont understand about command line arguments is what is the user supposed to input in my script?
what should the user do when the user runs the script? Thats what i do not understand
 
Old 08-26-2006, 05:47 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by kb100
i know what arguments are but i dont. i understand that the first arguments is $1 .... and so on $n but i dont understand
what an argument exactly does. What must a user input? Please give me an example it would make it so much easier.
Here is a very simple example of a script picking a single argument and using it.
Code:
#!/bin/sh
echo $1
Assuming its name is "myscript.sh", this script can be used that way:
Code:
./myscript.sh hello
I hope you won't have trouble understanding this example.
Quote:
I have read about 5 or 6 tutorials over 2 days.
You should limit yourself in reading less tutorials, and try understanding better what they explain.
Quote:
I know what an argument is but i dont understand what my script must do.
You script must do what your teacher asked.
 
Old 08-26-2006, 06:07 AM   #11
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
thanks for your patience. I know this must be annoying you. But im trying. This is my question:

the first argument is the name of the original file and the second argument is the new name for
the file.

Is this script right so far?

Code:
#!/bin/bash
echo "Enter filename"
read filename
echo "$1 is first argument"
filename=$1
echo "Enter name for new file
read filename2
echo "$2 is second argument"
filename2=$2
 
Old 08-26-2006, 06:14 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by kb100
thanks for your patience. I know this must be annoying you. But im trying. This is my question:

the first argument is the name of the original file and the second argument is the new name for
the file.

Is this script right so far?
Definitely wrong, you still miss what an argument is.

Either you prompt the user or you pick the arguments passed in the command line, but you are doing both ...
 
Old 08-26-2006, 06:26 AM   #13
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
But i have to prompt the user otherwise how will the user ask for anything? That is what i mean.
I do not understand what the script must do. The script should take two arguments. the first
argument is the name of the orginial file and the second is the new name for the file.
So what does that exactly want the user to do? nothing? Do you understand what i dont understand?

Sorry about this, it must be annoying for you.
 
Old 08-26-2006, 06:35 AM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I already gave you an example of a script taking one argument and using it in http://www.linuxquestions.org/questi...14#post2396014
Read it carefully, test the script I gave, then read again until you understand.
 
Old 08-26-2006, 06:51 AM   #15
kb100
Member
 
Registered: Aug 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Since i dont understand it i will go through it step by step and tel me if im wrong.
Thing is i am doing two question and both question are to do with arguments. so i need to do one
then im sure i could do the other.

Code:
#!/bin/sh
echo $1
Right this shows the first argument ($1). Right? But when i run the script its just blank.
 
  


Reply



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
scripting kb100 Programming 16 08-17-2006 06:41 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
scripting help please deepsix Programming 10 09-08-2005 07:49 PM
Need Help on Scripting wongedan Linux - Newbie 2 08-23-2004 08:11 PM
scripting help wedgeworth Linux - Software 10 09-25-2003 01:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 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
Open Source Consulting | Domain Registration