HardwareHeaven.com
Looking for the skin chooser?
 
 
  • Home

  • Reviews

  • Articles

  • News

  • Tools

  • GamingHeaven

  • Forums

  • Network

 

Go Back   HardwareHeaven.com > Forums > Software / Tools > Linux Operating Systems


Linux Operating Systems Talk about all Linux distributions and software here!

Reply
 
Thread Tools
Old Mar 31, 2005, 08:49 PM   #1
DriverHeaven Newbie
 
Join Date: Mar 2005
Posts: 1
Rep Power: 0
Nick84 is on a distinguished road

Thumbs Up! Can anyone help me with this, please...?

Hi felllas,

I am a new member in you company and I really need you help.

Can someone:

Write and explain a command that uses variable(s), the 'printf' command and backquotes (") in order to print the correct number of files in a directory within a descriptive sentence. For example: 'your current directory (/home/abc123) has 10 files'. Note that your command should be interoperable meaning that it should run correctly for any user without modifying the command itself.


Thank you very much
Nick84 is offline   Reply With Quote


Old Mar 31, 2005, 10:10 PM   #2
DriverHeaven Junior Member
 
Join Date: Oct 2004
Posts: 84
Rep Power: 0
Scar_T is on a distinguished road

I tried this command and it worked:

printf "Your current directory is %s, and it has %s file(s)" ${PWD} `ls -1 | wc -l`

Explanation:

printf - same that the C function printf.
PWD - it's a environment variable which it tells the current directory. To know the value of an env variable you have to put and $ and enclose the name of the variable between {}.

ls -1 | wc -l

This command (in fact two commands) tell you how many files are in the current directory. ls -1 list the files in the curent directory, the -1 option is for ls prints one file per line. wc is a command used generally to count words, but with the -l option it counts lines. So you need to pass to the output of "ls -1" to "wc -l" therefore you need to use the "|" connection pipe".

In order to be able to call a command inside a script line, you need to inclose this command between ``.
__________________
Scar_T - Parasite Coder
Scar_T is offline   Reply With Quote
Reply

Bookmarks

Thread Tools