ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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?
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
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
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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
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?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.