Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-14-2002, 02:40 PM   #1
LQ Newbie
Registered: Nov 2002
Location: UK
Posts: 2

Rep: Reputation: 0
Angry Scripting question

Sorry I know I probably sound VERY stupid but I'm in the process of writing a Linux script and I'm getting stuck! I'm totally new to this OS so it's kind of frustrating. I've managed to work out most of it from help sites and Linux tutorials but I have two questions that I just can't find the answers to anywhere!

1) Does anyone know how I can create a file that contains a list of all the files in the current directory? I can do a list of the files, that's simple enough, but how do I create a file with the list inside???

2) I need to write a script that reads in ten numbers (either one at a time during script execution or as parameters) and prints to the screen the sum of these numbers. This one REALLY has me stumped - I can't even figure out where to begin with this one so any help at all would be great!

I've been through so many Linux tutorials and help sites but all the jargen is sometimes too difficult for me to understand because, like I said, I'm completely new to this. If anyone could explain it in simple English i would be eternally indebted!

Many Thanks
Old 11-14-2002, 04:22 PM   #2
Senior Member
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,561

Rep: Reputation: 140Reputation: 140
1. type
ls -l >/home/aikitortois/list
this will send the output of ls -l to list in your home directory. if u want have it as a shell script. type it with an editor and give it
in the first line perl u would do that like

#!/usr/bin/perl -w

my $sum=0;
for (my $i=1; $i<11;$i++){
print "give me a number\n";
chomp(my $in = <STDIN>);
$sum += $in;
print "Sum is $sum\n";

u have to
chmod 0755 /urscriptfile
to make it executable
(get yourself a book about perl)
cheers jens
Old 11-14-2002, 04:37 PM   #3
LQ Newbie
Registered: Nov 2002
Location: UK
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you. That was very helpful. It's helped me improve some other stuff too, so cheers muchly!!
Old 11-14-2002, 05:18 PM   #4
Registered: May 2001
Posts: 29,383
Blog Entries: 55

Rep: Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558
Here's a Bash script example for the counting part.
Run as "sh <scriptname> [args]"

declare -i chars; declare -i sum
chars=($@); sum="0"
for i in ${chars[@]}; do sum="$sum + $i"; done
echo $sum; exit 0

D/l a copy of the Advanced Bash-Scripting Guide. Makes it easier for ya.

Btw, if you say you need to write the script, does it mean you don't want to yourself, but it's like for a school assignment?


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
scripting question... Basslord1124 Linux - Networking 2 11-09-2005 05:52 AM
Scripting question mithereal Linux - General 2 09-21-2005 02:02 AM
Scripting question msandford Linux - Newbie 4 09-05-2005 10:23 AM
scripting question gearoid Programming 3 05-26-2004 02:43 PM
Scripting question glock19 Linux - Software 2 09-05-2003 12:45 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration