Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Execute "echo $BASHPID" in Linux using Java

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am new to programming Java in Linux environment.

I need to get to execute "echo $BASHPID" in the terminal console of Linux and get the value returned.

I tried the following codes:



however, an error occurs when I execute the program. It does not recognized "echo $BASHPID". It throws "java.io.IOException: Cannot run program "echo $BASHPID": java.io.IOException: error=2, No such file or directory." Did I miss out a command function?
 
Saloon Keeper
Posts: 13391
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Erik. Take some time to read what the java.lang.Runtime documentation says on the various exec() methods.

Note that exec() does not work like the command prompt.
 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reminding me. I got the solution. I changed it to ProcessBuilder.

 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't see the value of this code... you are starting a new shell (bash), so you can ask the shell, what is its PID? What does that serve? The shell will terminate upon completion, so the PID is now for a terminated application. What can you do with the PID?

Henry
 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
I don't see the value of this code... you are starting a new shell (bash), so you can ask the shell, what is its PID? What does that serve? The shell will terminate upon completion, so the PID is now for a terminated application. What can you do with the PID?

Henry



Thanks, Henry, for pointing this one out. I am new to programming in Linux. I am trying to get the session id of a user in Linux. Would you know how to execute BASHPID in such a way that it gets the PID of the currently active user?
 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure $BASHPID is really what you want, this gives your the process ID of the current shell. Maybe you want $UID the current user id?

Also $BASHPID is just an environment variable so you can't "execute" it just get its value
 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernie Mcracken wrote:Are you sure $BASHPID is really what you want, this gives your the process ID of the current shell. Maybe you want $UID the current user id?

Also $BASHPID is just a system variable so you can't "execute" it just get its value



Thanks Ernie for dropping your comment! $UID is the current user id? I was hoping to get the user's session id. I was just thinking of this scenario: If there are 2 users using the same username and logs in from 2 remote machines to a separate Linux machine, their session user id will be their identifier because both would only return a single $UID. Would you happen to know how to get the user's session id?
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tty should give you a unique for every value instance of a login to a shell regardless of it the UID is the same (i think).
 
reply
    Bookmark Topic Watch Topic
  • New Topic