LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-13-2012, 11:40 PM   #1
mrh0550
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
scipting


can somebody anybody please help me with this!? I am completely lost and have tried everything...


Problem #1
Create a file in your home directory called
scripting.txt. Create a script that will copy
that file to a new file called scripting1.txt.

Problem #2
Create a script that will create a wizard for
the cp command. The user should be prmpted to
enter a source file and then prompted to enter
a detintation file. After that the cp command
should be executed and should copy the source
file to the destination file.

Problem #3
Create a script that will ask a shell user to
enter 3 numbers. After the numbers are entered,
add them together and display the output to the
user after clearing the screen.
 
Old 04-14-2012, 12:26 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by mrh0550 View Post
can somebody anybody please help me with this!? I am completely lost and have tried everything...


Problem #1
Create a file in your home directory called
scripting.txt. Create a script that will copy
that file to a new file called scripting1.txt.

Problem #2
Create a script that will create a wizard for
the cp command. The user should be prmpted to
enter a source file and then prompted to enter
a detintation file. After that the cp command
should be executed and should copy the source
file to the destination file.

Problem #3
Create a script that will ask a shell user to
enter 3 numbers. After the numbers are entered,
add them together and display the output to the
user after clearing the screen.

Dear mrh welcome to LQ. Your questions look like Home Work.

The questions you have asked are the basics of scripting.

You can start from here : http://tldp.org/LDP/Bash-Beginners-Guide/html/

Most of your question need user input for which you have to use read

like :

Quote:
echo "Enter a number: "
read A
So here the number you entered will be stored in the variabe A. So you can use this type store your source,destination,number and do any operation them.
 
Old 04-14-2012, 01:30 AM   #3
mrh0550
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
it is homework, and i understand the example you gave me, i just have no idea where to start
 
Old 04-14-2012, 01:50 AM   #4
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
The best starting point would be tutorial given by divyashree.
Bash is pretty simple comparing to real programming languages.

Problem #1
Create a file in your home directory called
scripting.txt. Create a script that will copy
that file to a new file called scripting1.txt.

Here you just need to copy file, created manually earlier.
An example could look like.

Code:
#!/bin/bash

cat scripting.txt > scripting1.txt # this prints the file, but instead of standard output which is usually console/terminal
you redirect output to file using ">", and because it doesnt exists it will be created.
But I rather suggest to use command created explicitly for copying files.
That shouldnt be hard to find.

Google - bash copy file
 
Old 04-14-2012, 02:09 AM   #5
mrh0550
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
thank you for your help, I have basic knowledge of c++ programming language, but I missed one night class of this course, and i'm just completely lost, i've read the tutorial and have been trying this whole time, but I don't understand it? am I supposed to write the script in vim? bahhh this is sooo confusing!!!!
 
Old 04-14-2012, 02:19 AM   #6
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
You can write script in whatever text editor you prefer. Good practice is to name file with ".sh"
extension but its not mandatory. Every bash script should start with #!/bin/bash,
this indicates what program should be used to interpret the script.
After you create your script do
Code:
chmod u+x myScript.sh
to give it executable permission, so you can run it by
Code:
./mySript
 
Old 04-14-2012, 03:14 AM   #7
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by mrh0550 View Post
it is homework, and i understand the example you gave me, i just have no idea where to start
You just start with learning some basic commands without which writing scripts is impossible.

Start from here:

http://wiki.linuxquestions.org/wiki/Commands
 
Old 04-14-2012, 03:22 AM   #8
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by mrh0550 View Post
thank you for your help, I have basic knowledge of c++ programming language, but I missed one night class of this course, and i'm just completely lost, i've read the tutorial and have been trying this whole time, but I don't understand it? am I supposed to write the script in vim? bahhh this is sooo confusing!!!!


You can use vi which is one of the powerful editor in *nix world and little complex for beginners.

You can also use editor like nano which are pretty simple to use.

Once you learn the basics in linux you will enjoy working in it.
 
Old 04-14-2012, 02:07 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Here's my usual list of bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

I highly recommend the first guide in particular for learning the basic concepts of scripting.

And since a script is just a text file, any text editor will do, although some like vim are more convenient in the long run due to code highlighting and other powerful features.

Beyond that, the only real way to learn is to dive in and start doing it. Take the individual actions and figure out how to do them, one step at a time. You should pick it up quickly enough, since you're already familiar with at least one other language. You'll certainly find that many of the basic concepts carry over.
 
Old 04-14-2012, 02:20 PM   #10
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
if you like join #uhelp at freenode.net
We've got a server, where we teach bash thingies in a shared console.
You connect to our server via ssh
and then we share a bash session with "screen -S <sessionname> -x"
 
Old 04-14-2012, 11:31 PM   #11
mrh0550
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
thank you everyone for all of your help! So i think ive got 1 and 2 finished... as for 3... no clue!! ?
 
Old 04-15-2012, 03:24 AM   #12
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by mrh0550 View Post
thank you everyone for all of your help! So i think ive got 1 and 2 finished... as for 3... no clue!! ?
Quote:
#!/bin/bash
let sum=$1+$2+$3
clear
echo "The sum of three numbers are $sum"
save it in a script, give +x to the script and execute like this: ./scriptname num1 num2 num3

Or interactively:

Code:
#!/bin/bash

echo "Enter the num1:"
read A
echo "Enter the num1:"
read B
echo "Enter the num1:"
read C

let sum=$A+$B+$C
clear
echo "The sum of three numbers are $sum"
 
Old 04-15-2012, 08:21 AM   #13
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
read -p "Enter three numbers divided by space:  " num1 num2 num3
echo $(( $num1 + $num2 + $num3 ))
 
Old 04-15-2012, 08:24 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
For debugging, I can highly recommend adding the following 'set' cmd
Code:
#!/bin/bash
set -xv
Here's slightly old but still good tutorial
http://rute.2038bug.com/index.html.gz
 
  


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
bash scipting khilly Linux - Newbie 6 11-04-2009 09:24 PM
Shell scipting namit Linux - Software 12 02-07-2006 05:57 AM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 06:42 AM
Bash Scipting ping question arkamir Linux - General 2 02-03-2004 08:18 PM


All times are GMT -5. The time now is 04:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration