Solved

Shell Scripting - how to get command successful result

Posted on 2008-10-16
6
483 Views
Last Modified: 2013-12-06
Hi,

I am trying to execute a touch command (am open to any other commands as well). In the shell script I want to perform something like this:

IF [ touch file1.txt ]
 THEN
      // File didn't exist but it has been created now
ELSE
      // File already exists
END

Now I know i can do, IF file_exists but I want to touch a file and ensure that very instant when I touched the file was created and it didn't exist before. Hence if I can't do IF file_exists and then touch.

I want to execute a command (any command, please suggest) that will create the file and along with that return a result to a IF statement informing it about the file creation, else if the file existed then it must not be recreated.

I know this is a little convoluted explanation but it would really help if you can answer this, please ask any clarification questions as well.
0
Comment
Question by:infa007
6 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 22737230
try

if [ -f file1.txt ]
then
      echo file exists
else
      touch file1.txt
      echo file created
fi

0
 

Author Comment

by:infa007
ID: 22737353
Thanks, thats exactly what I don't want to do ... consider a php programming solution ...

function abc($x) {
 
if($x == 1)
 return 0;
else
 return 1;
}

// Here is what I want to do from Unix

if(abc(2)) {
// received 1
}
else {
// received 0
}

Instead of calling abc function I want to, call a command, e.g. touch, and require it to let me know if the touch of the file was successful (while not overwriting the file)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22738530
touch does not over write any file. it can be used to change the creation / modification time.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 22738561
But touch doesn't indicate (by returncode) whether the file already existed or not. I think, such a functionality is what infa007 is searching for.
I, personally, don't know any utility with that functionality.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22739005
that's why there is an if then
0
 
LVL 14

Accepted Solution

by:
sjm_ee earned 500 total points
ID: 22741921
The questioner states that "ensure that very instant when I touched the file was created and it didn't exist before". So what the questioner seems to be trying to do is to use the file as a semaphore, presumeably to create a critical section within the code so that it is only executed once at a time.

If this is correct, then you should understand that file creation and deletion are atomic operations under AIX, UNIX and many OSes, particularly those with journalled file systems. However the atomic file system equivalent of "test and set" is not available from any command that I am aware of. You can get this behaviour in C with "open(O_CREAT|O_EXCL)".

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sudo logging 5 81
nodeip 9 75
what is the difference between "sudo su" and "su - root" 6 107
unix in java example 9 55
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now