Can someone interpret this -- not sure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can someone interpret this -- not sure
# 1  
Old 05-06-2010
Can someone interpret this -- not sure

Was wondering if someone could interpret this for me -- I'm not sure what everything means. It's a shell script from my bash book:

Code:
cd ()
{ 
     builtin cd "$@"
     es=$?
     echo "$OLDPWD ->$PWD"
     return $es
}

what I don't quite understand is the "$@". I think, if I understand it correctly, it is all the positional parameters to the function, as separate double quoted strings separated by spaces.

The function works fine, but how does it interpret a directory with more than one word? Is each word in the directory name one positional parameter or is the whole directory name one positional parameter?

Curiously, if I change "$@" to "$*" the function works as well, so what's the difference as far as the program is concerned? I know that "$*" is all the positional parameters, taken as one string.

Last edited by Scott; 05-06-2010 at 08:20 PM.. Reason: Code tags please...
# 2  
Old 05-06-2010
Quote:
The function works fine, but how does it interpret a directory with more than one word? Is each word in the directory name one positional parameter or is the whole directory name one positional parameter?
I am not sure what you mean by "a directory with more than one word"? Can you provide an example?
# 3  
Old 05-07-2010
I mean a directory with just that more than one word in its name -- say, for example my\ documents/ as opposed to just documents/
# 4  
Old 05-07-2010
Quote:
Originally Posted by Straitsfan
so what's the difference as far as the program is concerned
In this particular case, there is none ...

Code:
[house@leonov] more test.bash
#! /bin/bash
echo "\$@: '$@'"
echo "\$*: '$*'"
exit 0
[house@leonov] bash test.bash some folder
$@: 'some folder'
$*: 'some folder'

... but, of course, there are other cases Smilie
# 5  
Old 05-07-2010
In this special case there is no effective difference between "$*" and "$@", because in bash the cd builtin accepts only one argument, the name of the directory you want to change to.

In ksh, cd takes optionally a second argument.
Code:
cd old new

where old ist substituted with new in the current directory name and then changed to this directory, like

Code:
$ pwd
/opt/software/app1/prod/install
$ cd app1 app2
/opt/software/app2/prod/install
$ pwd
/opt/software/app2/prod/install

In this special case it would make a difference, because "$@" would keep two arguments intact, even if they contain whitespace, whereas "$*" would combine arg1 and arg2 into one string.
# 6  
Old 05-07-2010
Quote:
Originally Posted by Straitsfan
I mean a directory with just that more than one word in its name -- say, for example my\ documents/ as opposed to just documents/
It's not more than one word if you quote it. In that case, there is no field splitting done and it will always be one word (word in the bash man page sense) which happens to contain a space.


Quote:
Originally Posted by hergp
In this special case there is no effective difference between "$*" and "$@", because in bash the cd builtin accepts only one argument, the name of the directory you want to change to.
That is absolutely incorrect. If there are multiple arguments passed to a script or function, the double-quoted values "$*" and "$@" will ALWAYS differ. The former will always expand to one string, the latter to multiple strings.

If this function were called with two arguments, for example, it would try to cd into a directory named "$1" (as you pointed out, bash's built-in cd would ignore $2 and any other arguments). However, if the code were using "$*" instead, the bash builtin would be passed a single argument whose value would be "$1 $2". Definitely, not the same.

In this case, what is equivalent is the use "$@" and "$1", since bash's cd will ignore all other arguments.

Regards,
Alister
# 7  
Old 05-07-2010
Alister.

I was not talking about scripts and functions in general, but this special case. And if this function is called properly, then there is only one argument.

Quote:
Originally Posted by alister
That is absolutely incorrect. If there are multiple arguments passed to a script or function, the double-quoted values "$*" and "$@" will ALWAYS differ.
Thats right, but this function does only have one argument. If more than one argument is passed, it is just a plain syntax error, which should be handled properly (which bash should do too - ignoring a syntax error doesn't seem to be a very good implementation).

When you call ksh's cd function with extra arguments (in this case a third one), it says
Code:
ksh: cd: bad argument count

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Who -r interpret?

I booted into single user mode with /usr/sbin/reboot -- -s but after doing a control -d my who -r shows run-level 3 Nov 17 14:07 3 0 S I was expecting it to show run-level S why is this still in run level 3? thanks (1 Reply)
Discussion started by: goya
1 Replies

2. UNIX for Dummies Questions & Answers

Need to interpret code

hi All, i have never used sed in Unix environment, but i have one script which is using this following command: cat audit_session_rpt_MSP_20140331.lst|sed -n '/Apr 14/!p'| sed -n '/Page/!p'| sed -n '/UserName/!p' |\ egrep -v '^-|^=|^\*'|sed '/^$/d'|sed -e '1,7d'... (1 Reply)
Discussion started by: lovelysethii
1 Replies

3. Shell Programming and Scripting

don't know how to interpret this

Can anyone tell me how to interpret this: listpage="ls |more" (the spaces are there in the example) $listpage It's from my bash book and I'm not sure what it means (3 Replies)
Discussion started by: Straitsfan
3 Replies

4. UNIX for Dummies Questions & Answers

How does Awk interpret $0!~

I know $0 is the entire file's contents (at least I think that is what it is!), but what exactly is: $0!~ This was a snippet from a larger line awk '$0!~/^$/ {print $0}' This deletes blank lines, but I want to know specifically the $0!~ part... I am guessing /^$/ is regex for blank line...... (5 Replies)
Discussion started by: glev2005
5 Replies

5. High Performance Computing

How to interpret Papi output

I have collected data of Number of L2 cache misses using PAPI. I had run an MPI application with 4 threads (mpirun -np 4) and each thread reads the cache misses in L2. Each thread outputs data for every timestamp. eg: Timestamp data xxx530 thread# 0 2136 xxx531 thread# 0 ... (0 Replies)
Discussion started by: vishwamitra
0 Replies

6. Shell Programming and Scripting

Interpret a text file

hi I have a text file abc.txt as below a = 0 b = 1 c = 3 i want to interpret this file i.e. if number corresponding to 'a' is 0 i want to run a script script.bash . How do do that? (4 Replies)
Discussion started by: shishirkotkar
4 Replies

7. UNIX for Dummies Questions & Answers

How to interpret TOP

Hi, So I am new to Unix, and I need to check the performance of some apps I am running. But I don't know how to interpret the output from TOP. Could somebody please explain the difference between the different values. And also explain how I can have a process which has a %CPU > 100? ... (7 Replies)
Discussion started by: dj_jay_smith
7 Replies

8. Shell Programming and Scripting

Interpret the sed command.

Could you interpret the following sed and awk command for me? command: cat tempfile2 |sed "s/\(BUILD-3-.*-\.-\)\(.*\..*\..*\)/\2/" | awk '{printf "%-8.8s %-23.23s %-30.30s %-50.50s\n", $1,$2,$3,substr($0,index($0,$4))}' > outfile2 2>/dev/null input:(data in tempfile2)... (1 Reply)
Discussion started by: vj8436
1 Replies

9. UNIX for Dummies Questions & Answers

Please interpret.

Hi guys, I have no idea on unix but suddenly, my cobol programs calls a unix script that i know nothing about. can you guys interpret these lines for me? i know its a print command but I want to actually know how many copies it prints. qprt -da -P $1 -t '6' -i '6' -l '70' $2 qprt -da... (1 Reply)
Discussion started by: supacow
1 Replies

10. Shell Programming and Scripting

How Do We Interpret This ?

ksh $ETL_XXX/bin/filename.ksh wf_workflowname . Which is used in post session command. (2 Replies)
Discussion started by: dummy_needhelp
2 Replies
Login or Register to Ask a Question