LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-09-2009, 07:08 PM   #1
linux_newbie79
Member
 
Registered: Apr 2009
Posts: 36

Rep: Reputation: 15
shell scripting


Hi,

I like to know low level details of the following script.

cmp -s $1 $2
differ = $?

In the above script, the first line will be executed and the result will be stored in the variable differ.

my understanding is that the shell sees each line and fork/exec each command. The second like blocks the shell in order to get the outcome of previous line. Is my understanding correct?

Thanks in Advance
 
Old 08-09-2009, 07:16 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
When a program runs, it stores its final exit value in the shell var $? .
You can either check that value immediately, or store it in another var eg

result=$?

note no spaces around the '=' symbol.
Read these:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Cmds are processed sequentially, so each has to wait for the preceding cmd to complete. You can get around that by using the '&' symbol , which means 'to background the program' eg

myprog &

and the next cmd will be executed without waiting for completion of the background task.
You can get the pid of the background task from the shell var $!
 
Old 08-09-2009, 08:00 PM   #3
linux_newbie79
Member
 
Registered: Apr 2009
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for your reply.

So, in the script, if I call a user process which has nothing but a while loop, next line in the script will not be executed.
 
Old 08-09-2009, 08:08 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
I'd prefer an example to be clear, but fundamentally, all prog langs run code consecutively unless you go out of your way to do something else, often known as multi-processing or multi-threading.
Have a good read of those links, and more importantly, just try stuff(!), its much faster than keep asking qns and waiting for a reply.
Do however come back if you get stuck; show your code and we'll try to help.
Also, add your distro & version to your profile for better help.
 
Old 08-10-2009, 06:45 AM   #5
jeromeNP7
Member
 
Registered: Jun 2009
Posts: 101

Rep: Reputation: 19
Your sample can also be written as below by using so-called backticks:
differ=`cmp -s $1 $2`
This way you know exactly where differ gets it's value from.

Linux

Last edited by jeromeNP7; 09-04-2009 at 09:22 PM.
 
Old 08-10-2009, 07:01 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
I will often use the form:
cmp $file1 $file2 && <do something>

You could also use
cmp $file1 $file2 && <do something> || <do something else>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 08:18 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM


All times are GMT -5. The time now is 07:55 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration