Shell Scripting - how to get command successful result

Posted on 2008-10-16
Last Modified: 2013-12-06

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 ]
      // File didn't exist but it has been created now
      // File already exists

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.
Question by:infa007
LVL 40

Expert Comment

ID: 22737230

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


Author Comment

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;
 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)
LVL 40

Expert Comment

ID: 22738530
touch does not over write any file. it can be used to change the creation / modification time.
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 68

Expert Comment

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.
LVL 40

Expert Comment

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

Accepted Solution

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)".


Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

706 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

15 Experts available now in Live!

Get 1:1 Help Now