Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Execute "echo $BASHPID" in Linux using Java

 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for reminding me. I got the solution. I changed it to ProcessBuilder.

 
Henry Wong
author
Marshal
Pie
Posts: 21218
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic