Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Programming in Linux _ Scripting Help

Posted by: ChrisSmith2002 Mar 10 2015, 05:59 PM

I need help on a project that I am trying to do for class. I just can't seem get it, everything that I'm doing errors out. I know that I need to use a loop of some kind and that I need to ls | wc -l, but no matter what I try I can't get it to work ... sad.gif please help if you can.


1. Script should accept a folder name as an argument.
2. The script should determine the number of files in the folder given as an argument in #1
3. Further, the script should only list the first half of the files( so if there are 6 files, only have it list the first 3)
4. When listing the file names, they should be in the following structure.

"1- this file "x1" is in the top half of the folder list"
"2- this file "x2" is in the top half of the folder list"

note- "x1" and "x2" represent the file names

5. Also, the program should show the number of files in folder before listing the files.


Posted by: michaelk Mar 10 2015, 06:53 PM


We will help but do not expect others to do your assignments. So post what you have written so far.

Posted by: ChrisSmith2002 Mar 10 2015, 07:18 PM

Alright, it's a mess... so here it goes. We had about a 5 minute lesson before the assignment is given out and this is what I have gotten. Please don't get my intentions wrong, I've been researching online and trying this for a few hours now... This is an intro to linux class and it's only been a few weeks.

Unsure of how to pass the file name into it either

#! /bin/bash

echo "the number of files in this folder is" ls | wc -l

count='ls | wc -l' (which doesn't work)
index= (count/2)

while [ $count -lt $index ]; do
echo" $index- This file is in the top half of the folder list"
index=$(( $index + 1))

Posted by: michaelk Mar 11 2015, 09:43 AM

In a nutshell command line arguments are $1...$9, $[10]....

./myprog test test1 test2

$1=test, $2=test2 etc.

Lots more info on command line arguments on the internet.

double quote ("), single quote(') and backtick (`) i.e. the key to the left of the one on US keyboards are different. To run a command you need to use the backtick vs single quote. Hwever, the perfered programming technique is to use the $() i.e.

count=$( ls | wc -l ) vs count=`ls | wc -l`

spaces are very import in bash. Lots of good infomation on bash math can be found on the internet too.

index= (count/2)
index=$(( $index + 1))

Powered by Invision Power Board (
© Invision Power Services (