5 Replies - 704 Views - Last Post: 24 October 2012 - 02:17 PM Rate Topic: -----

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

How to echo certain items in a directory with bash?

Posted 24 October 2012 - 06:54 AM

Recently, I started learning bash, and I'm pretty new at it. I have the below bash script in my home directory, and when I run it, it simply prints out each file and directory in my home directory in the format "item:(name of the file/directory)", which is exactly what I want. But now I want to be able to give two parameters to my script, from which both of them will be intigers, and first one will be the starting index and the second one ending index.

So let's say that my script is called item.sh, and if I call it like "./item.sh 3 5", it should simply print me the items in my directory, but starting from the third item, and printing until the fifth one. So it should print third, fourth and fifth items only. Let's say that I have this as the output of my unedited script:

item:Desktop
item:Development
item:Documents
item:Downloads
item:Dropbox
item:Eclipse
item:examples.desktop
item:git
item:grub.txt
item:hw.c~
item:hw.sh
item:loops.sh
item:Music
item:Pictures
item:Progex-Cloud
item:Public
item:public_html
item:rvm.sh
item:Softwares
item:Templates
item:trunk
item:Ubuntu
item:One
item:Videos



Then after editing my script and executing it like: "./item.sh 3 5", I want the output to be:

item:Documents
item:Downloads
item:Dropbox



For my initial program, I have the following script, which works just fine:

#!/bin/bash
for i in $( ls );
do
      echo item:$i
done



But when it comes to adding two parameters to my original script and achieving the needed output, I'm having difficulties. I have tried many things, but simply it doesn't work. Seems like I cannot find a proper algorihtm. If someone could give me just one working version, I would be glad. Lastly, I have something like this, which doesn't work as needed of course, and uses just one parameter.

#!/bin/bash
COUNTER=$1
for COUNTER in $( ls );
do
	echo item:$COUNTER
done


This post has been edited by erkant: 24 October 2012 - 06:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to echo certain items in a directory with bash?

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: How to echo certain items in a directory with bash?

Posted 24 October 2012 - 07:55 AM

Your modified script could just call your original script and pipe it through tail and head.
Was This Post Helpful? 1
  • +
  • -

#3 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: How to echo certain items in a directory with bash?

Posted 24 October 2012 - 09:49 AM

View Postsepp2k, on 24 October 2012 - 07:55 AM, said:

Your modified script could just call your original script and pipe it through tail and head.


Can you show me how can I do it? Or at least point me somewhere, while as I said I'm pretty new to Bash, and have minimum knowledge with it, and haven't tried pipeline before.

This post has been edited by erkant: 24 October 2012 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: How to echo certain items in a directory with bash?

Posted 24 October 2012 - 10:03 AM

Do you know how to use pipes? If so, all you need to know is how to use head and tail. Those commands have man pages (which you can read by typing man head and man tail respectively - in case you don't know about man pages yet), but the only thing you care about is the -n option, really:

From man head:

Quote

-n, --lines=[-]K
print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file


From man tail:

Quote

-n, --lines=K
output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth

Was This Post Helpful? 1
  • +
  • -

#5 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: How to echo certain items in a directory with bash?

Posted 24 October 2012 - 01:50 PM

I don't know anything about pipes, that's why I wanted to see how it can be implemented. If you could make the needed changes to my code, and show me how I need to execute it with pipes and stuff I would be glad.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: How to echo certain items in a directory with bash?

Posted 24 October 2012 - 02:17 PM

A pipe takes the output from one program and feeds it as input to another program. So if you write app1 | app2, app1 runs, but instead of going to the screen, its output goes as input into app2 and the output of app2 is what finally goes to the screen.

So since head and tail read data from standard input when they're called without any file names, you can pipe the output of your script into head and the output of head into tail, to only display a certain window of entries.

For more information, there are plenty of resources on how pipes work on the Internet.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1