LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2011, 02:57 AM   #1
moata_u
Member
 
Registered: Jan 2011
Posts: 32

Rep: Reputation: 0
Smile error handling shell script


i just have code that i cant understand :

log() {
local label=$1
shift
$("$@")
if [[ $? == 0 ]]; then
status=OK
else
status=ERRPR
fi
echo "$label : status : $status"
}
*****what does $("$@") need for ?? and what is different if i use $("$*")

Last edited by moata_u; 03-09-2011 at 03:07 AM.
 
Old 03-09-2011, 04:05 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
$("$@") substitutes all the arguments and execute them as a command in command substitution. Since the result of the command resides alone on its own line, it is further executed as a command. This may bring to errors.

Example: suppose the list of arguments is "ls my_dir" and my_dir is a directory containing a file called "testfile". First the shell performs parameter expansion by substituting $@ with "ls my_dir":
Code:
$(ls my_dir)
then it performs command substitution and the statement becomes:
Code:
testfile
finally it tries to execute testfile as a command, which obviously can trigger a "command not found" error. I think the whole thing serves to log the status of a command without showing the output. In my opinion it would be more correct to assign the result of the command to a dummy (unused) variable. For example:
Code:
log() {
  local label=$1
  shift
  dummy=$("$@")
  if [[ $? == 0 ]]
  then
    status=OK
  else
    status=ERRPR
  fi
  echo "$label : status : $status"
}
The log function will be invoked with an identifying label and the command to execute along with its options and arguments. The label will appear in the output but it will not be executed in command substitution, since the shift statement makes the shell to skip it. Example:
Code:
log "Disk Space" df -k
Regarding the difference between $@ and $*, the former preserve spaces in the arguments so that if you have two arguments, "some string" and "another word", they will be seen as two, whereas $* will see them as four. A last thought is about the order of expansion: the shell performs different types of expansion and they are made in a strict order. Ignoring it can bring to unpredictable behavior in some circumstances. Here is a complete list of expansion in the order they are executed. Hope this helps.
 
1 members found this post helpful.
Old 03-09-2011, 09:03 AM   #3
moata_u
Member
 
Registered: Jan 2011
Posts: 32

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks man now every thing is clear 100%
 
  


Reply

Tags
bash scripting, command



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
handling linux kernel crash using a shell script indu.a Linux - Kernel 1 03-22-2007 01:29 PM
handling linux kernel crash using a shell script indu.a Programming 1 03-22-2007 06:43 AM
file handling shell script ankit_mcgill Programming 6 04-29-2006 12:32 AM
Shell Script.. Date format handling C-RAF. Programming 2 02-14-2006 08:34 AM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration