473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Saving `test` results in a variable

63
Is there any way to save the result of a "test" command in a variable, or would I have to do something like:

bool=`if [ -d . ]; then echo 1; else echo 0; fi;`
Nov 30 '06 #1
1 1452
michaelb
534 Expert 512MB
Shell already does it for you.
In Bourne shell the result of the last executed command is saved in variable $?
In C shell it's a built-in variable called status.

Here's the sample script:

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. [ -d `pwd` ] 
  3. echo $?
  4. # the statement above should print 0, (success status) 
  5. # because your current directory is obviously a directory.
  6.  
  7. [ -d /dev/nonsence ] 
  8. echo $?
  9. # the statement above will likely to print 1 (failure), 
  10. # it's unlikely that this directory exists
Note that 0 is always a success, but shell may return other failure codes,
so most often you're only interested to see whether you gor 0 or not:

exec some shell command, then test the result:
if [ "$?" -ne 0 ] ; then
... ...

It is a common practice to include $? in double-quotes, as shown above.
Hope it helps.
Dec 2 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lawrence | last post by:
I posted before, but have now narrowed my problem down to this method. At the start of the method, I test to make sure that I have a resource, a pointer to data returned from a database. This test...
4
by: John Kandell | last post by:
Hi, I posted this in the asp.net group, but didn't get a response. Maybe someone here can help me with this... --- Would someone be able to shed some light on what is the cost of saving a...
3
by: jad101 | last post by:
I have an asp.net web page that: 1. Shows a message 2. Posts back (using javascript) 3. Reads contents of a file 4. response.write the contents of the file to the browser My issue is this: ...
3
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
27
by: Josh | last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI screens) that we want to get out of VB and into a better language. The program is over 10 years old and has already been...
0
by: Jan Smit | last post by:
Hey, In Visual Studio the default load test results store is a SQL Express database. In the help files of visual studio, under the section 'About the Load Test Results Store', there is a note...
9
by: hendedav | last post by:
Gang, I am trying to get a regular expression test to work and can't figure out why. I will give you the code below: for (var j=0; j<document.getElementById('cmbList').options.length; j+...
6
by: John Kotuby | last post by:
Hi all, I am using a 3rd party program in a VS2005 web project. The tool takes as input a string containing HTML and converts it to RTF. I have been creating a page by dynamically loading...
27
by: RobG | last post by:
I was investigating a function to determine whether daylight saving was being observed on a particular date (given the platform's regional settings) and came across a suggestion at merlyn.com to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.