what does this piece of code do?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting what does this piece of code do?
# 1  
Old 06-05-2006
what does this piece of code do?

Hi All,

I am trying to understand and change some code written by some programmer a while ago. There are following three lines of code that I am unable to grasp. Could anybody please help me understand it?

1) cd - > /dev/null

2) fname=`basename "$1"` where $1 = /dirA/dirB/a.txt
dname="${1%/$fname}"

I understand in this case fname would give me a.txt but what will dname be? I tried to echo dname on command line but it comes out to be blank

3) var1=/xx/yy
var2=${var1##*/}1

The result of the above is yy1. How does var2 get set to yy1?

4) inUse=`fuser "$1" 2>&1`
outputLines=`cat <<! | wc -l
${inUse}
!`

What I dont understand in above code is, if the intent of the programmer is just to count the number of rows returned by inUse variable, why did she choose to have <<! ?? Is there a good reason behind it or is just too complex without any good reason?

Thanks guyz.

Last edited by Vikas Sood; 06-05-2006 at 05:07 PM..
# 2  
Old 06-05-2006
1) Change to the previous directory and redirect any output to the bit bucket.
2) dname takes the fully qualified filename and removes "/a.txt" leaving only the path.

e.g. "/dirA/dirB"

3) By removing all characters to the left of the last "/" and including the "/" and appending "1" to it. This uses the shell built-in pattern matching capabilities and effectively strips off all text to and including the last "/" character.
# 3  
Old 06-05-2006
thanks

Hi
Thanks for your reply. I had edited the post to include a fouth line of code? Could you plelase help me understand that as well?
TIA
Vikas.
# 4  
Old 06-05-2006
4) No doubt that the back ticks would have been messed up in the here document and this is the easiest implementation for her. inUse is running the fuser command and returning its output into the inUse variable. Then a count is determined by using cat on a here document on the previous command's output. This is done no doubt to accomplish the tasks without creating temporary files.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a piece of shell scripting to remove column from a csv file

Hi, I need to remove first column from a csv file and i can do this by using below command. cut -f1 -d, --complement Mytest.csv I need to implement this in shell scripting, Whenever i am using the above command alone in command line it is working fine. I have 5 files in my directory and... (3 Replies)
Discussion started by: Samah
3 Replies

2. Shell Programming and Scripting

Time:Piece

Hi I am trying to find out difference between two dates with Time:Piece. I am able to get the days difference, but I want the Days as wells as hours, mins and sec difference. Below is my code for both, but the later is now working. Can anyone help me out. With only days use... (0 Replies)
Discussion started by: sauravrout
0 Replies

3. Programming

what is the name of this piece of code

while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0) if (write(outputFd, buf, numRead) != numRead) fatal("couldn't write whole buffer"); if (numRead == -1) errExit("read"); if (close(inputFd) == -1) errExit("close input"); if (close(outputFd) == -1) errExit("close output"); ... (1 Reply)
Discussion started by: fwrlfo
1 Replies

4. Shell Programming and Scripting

Looking for guidance (comments) on a piece of code

Hello -- I am trying to learn to do a little sed and awk scripting to search for text and numbers in text files (text processing/manipulation). My professor gave me a piece of uncommented code and I am very unfamiliar w/ the language. Can someone help me with comments so I can understand what is... (2 Replies)
Discussion started by: smithan05
2 Replies

5. War Stories

One time I fixed an LCD monitor with a folded piece of paper

Some of the colors weren't working on the Monitor. I found pressing around the plastic border of the screen brought them back. I opened the monitor casing and used the folded paper to put pressure against the LCD panel and housing. Wah Lah. More of a bend than a hack I guess. (2 Replies)
Discussion started by: herot
2 Replies

6. Shell Programming and Scripting

Need help optimizing this piece of code (Shell script Busybox)

I am looking for suggestions on how I could possibly optimized that piece of code where most of the time is spend on this script. In a nutshell this is a script that creates an xml file(s) based on certain criteria that will be used by a movie jukebox. Example of data: $SORTEDTMP= it is a... (16 Replies)
Discussion started by: snappy46
16 Replies

7. Shell Programming and Scripting

script or piece of code where the data returned by a stored procedure you are writing

hi fndz. Can you please help me with the code if I call a stored procedure from my shell script and stored procedure returns a cursor, cursor output should be saved to a file (3 Replies)
Discussion started by: enigma_83
3 Replies

8. Shell Programming and Scripting

How to extract a piece of information from a huge file

Hello All, I need some assistance to extract a piece of information from a huge file. The file is like this one : database information ccccccccccccccccc ccccccccccccccccc ccccccccccccccccc ccccccccccccccccc os information cccccccccccccccccc cccccccccccccccccc... (2 Replies)
Discussion started by: Marcor
2 Replies

9. Shell Programming and Scripting

a piece of code, plz help to review

use "getopts" to get params from command. Need replace black with a specified string like "%20 DEFAULT_DELIM=%20 ... while getopts dek:f:t:vh OPTION do case $OPTION in t) DELIM=`tvar=/'"$OPTARG"'/ svar="$DEFAULT_DELIM" awk 'BEGIN{T=ENVIRON;S=ENVIRON; while(index(T,S)!=0){S=S"0"};print... (0 Replies)
Discussion started by: anypager
0 Replies
Login or Register to Ask a Question