LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script help (https://www.linuxquestions.org/questions/linux-newbie-8/script-help-381770/)

zyphonic 11-10-2005 08:41 AM

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

nx5000 11-10-2005 08:49 AM

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

zyphonic 11-10-2005 08:56 AM

thanx for ur help. What does man bash, man wc mean?

nx5000 11-10-2005 09:15 AM

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/

zyphonic 11-10-2005 09:24 AM

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!

Nawar 11-10-2005 11:08 AM

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


All times are GMT -5. The time now is 09:30 PM.