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 11-10-2005, 09:41 AM   #1
zyphonic
LQ Newbie
 
Registered: Oct 2005
Posts: 9

Rep: Reputation: 0
Unhappy Script help


How do u write a script that provides a file, let say "MyFiles", that contains a list of all the files in the current directory and displays on the screen the number of files present in the current directory??

thanx
 
Old 11-10-2005, 09:49 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
Re: Script help

Quote:
Originally posted by zyphonic
How do u write a script that provides a file, let say "MyFiles", that contains a list of all the files in the current directory and displays on the screen the number of files present in the current directory??

thanx
ls -1A | tee MyFiles | wc -l


In two ways:
ls -1A > MyFiles
ls -1A | wc -l

wc counts word,lines,characters from the input . -l gives you only the lines

man bash
man wc

have fun
 
Old 11-10-2005, 09:56 AM   #3
zyphonic
LQ Newbie
 
Registered: Oct 2005
Posts: 9

Original Poster
Rep: Reputation: 0
thanx for ur help. What does man bash, man wc mean?
 
Old 11-10-2005, 10:15 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
man is a unix command which stands for manual, it gives you access to reference manual of all the commands you have on your systems, and a lot of other things.

Code:
man wc
will show you the manual of the command "wc" .

You can move in with the arrows, move one page forward with space , move one page backwards with 'b' , quit with 'q' , search by typing '/' followed by what you want to search (then 'n' for next found, 'p' for previous found)

->
you can sometimes also try :

Code:
apropos wc
, it will give you all the manual page which refers to wc , or all commands containing the string "wc"
Code:
apropos -e wc
wc will give you only the manual pages having exactly "wc" in their description

->
an also

Code:
info coreutils wc
You could print this page in have a look at it.
http://www.indiana.edu/~uitspubs/b017/

Last edited by nx5000; 11-10-2005 at 10:18 AM.
 
Old 11-10-2005, 10:24 AM   #5
zyphonic
LQ Newbie
 
Registered: Oct 2005
Posts: 9

Original Poster
Rep: Reputation: 0
It is really great that u r helpin me get to grips with linux. Could you help me with a few more scripting questions?

There are a couple of things I wanna b able to do in linux put am findin it difficult:

(1)

How do i write a script that moves all the files that end in ".txt" from the current directory to another (target) directory. I want this script to produce a text file ("filesCopied") that lists all files that have been moved.

(2)

How do i 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?

(3)

How do i alter the previous script to allow it to work for arbitrary lists of numbers.

Thanks alot nx5000!
 
Old 11-10-2005, 12:08 PM   #6
Nawar
Member
 
Registered: Nov 2005
Location: Midwest America
Distribution: SuSE, RedHat
Posts: 39

Rep: Reputation: 15
Talking

Just who's homework assignment are we doing here?

LOL -

For #1, did you mean to "copy" or "move" - since you wanted the filename generated to be"filesCopied", I'll assume you meant copy.

#1 Starts below

#!/bin/bash
[ -d ${1} ] && cp $( ls *.txt | tee filesCopied ) ${1} || echo "Invalid directory name entered".

---------------------------------------------------
#2 starts below

#!/bin/bash

viNumParams=${#}
viMathExpr="0"
[ ${viNumParams} -gt 10 ] && {
echo "Only 10 parameters are allowed, you have entered ${viNumParams}"
exit 1
}

viCounter=1
while [ ${viCounter} -le ${viNumParams} ]
do
vsMathExpr=${vsMathExpr}" + ${viCounter}"
viCounter=$(( ${viCounter} + 1))
done

while [ ${viCounter} -le 10 ]
do
echo "Enter numeric for number ${viCounter} parameter: \c"
read viNumeric
vsMathExpr=${vsMathExpr}" + ${viNumeric}"
viCounter=$(( ${viCounter} + 1))
viNumeric=0
done

echo "Here is the completed math expression: "${vsMathExpr}
echo "Here is the answer: "$(( ${vsMathExpr} ))

---------------------------------------------------
#3 starts below

#!/bin/bash

viNumParams=${#}
viMathExpr="0"

viCounter=1
while [ ${viCounter} -le ${viNumParams} ]
do
vsMathExpr=${vsMathExpr}" + ${viCounter}"
viCounter=$(( ${viCounter} + 1))
done

vsNumeric="0"

while [ "${vsNumeric}" != "" ]
do
echo "Enter numeric for number ${viCounter} parameter, press enter to end list: \c"
read vsNumeric
[ "${vsNumeric}" != "" ] && vsMathExpr=${vsMathExpr}" + ${vsNumeric}"
viCounter=$(( ${viCounter} + 1))
done

echo "Here is the completed math expression: "${vsMathExpr}
echo "Here is the answer: "$(( ${vsMathExpr} ))

----------------------------

HTH,

Nawar

Last edited by Nawar; 11-10-2005 at 12:23 PM.
 
  


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
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 11:23 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 05:21 PM


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