LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-14-2002, 03:40 PM   #1
aikitortoise
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
Jamie
 
Old 11-14-2002, 05:22 PM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,550

Rep: Reputation: 137Reputation: 137
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
#!/usr/bin/sh
in the first line

2.in 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, 05:37 PM   #3
aikitortoise
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!!
x
 
Old 11-14-2002, 06:18 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

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

#!/bin/bash
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?
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:57 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