in reply to One liner to test existance of a file

"perl -e /.firstboot | echo $?"

has a number of problems.

The above should be

q{perl -e'exit(-e "/.firstboot" ?1:0)' | echo $?}

which can be simplified to

q{perl -e'print -e "/.firstboot" ?1:0'}

But why not just use the shell?

q{test ! -e /.firstboot ; echo $?} -or- q{if [ -e /.firstboot ] ; then echo 1 ; else echo 0 ; fi}

Also, you didn't specify what should be returned when it cannot be determined whether the file exists or not.

Replies are listed 'Best First'.
Re^2: One liner to test existance of a file
by kamal (Sexton) on Jul 15, 2010 at 01:02 UTC
    if i use
    q{if [ -e /.firstboot ] ; then echo 1 ; else echo 0 ; fi}
    i get:

    0 root@baseline-dc1:~#

    The required output should be: 0 with NO new line, or carriage return, or space. somehow (i think) due to Net::SSH::Expect the prompt of the remote system is being appended to $stat hence i need to remove it
      That command does not output that at all. -n prevents echo from outputting a newline. As for the prompt, it comes from the shell. Don't run a shell interactively, hide its prompt, waitfor it, or exec a shell to execute the command, causing the connection to drop after the output has been obtained.