I am on UBUNTU 12.04.
I have a shell script that logs the output of a certain processes. The process being logged changes depending on the parent shell script that called the logging script. I would like to write the output of this logging to a file on disk, but am having trouble naming the file.
I would love to name it according to the parent script's own name. Unfortunately I have absolutely no idea how to get that name from script. I imagine that the best way to do that would be to extract the parent PID ($PPID), and then use that to find the script name, but I also don't know how to do that.
Instead of jumping through hoops to do this, just have the parent open the log on say FD4. Then whenever you need to log, just write to FD4.
# parent script
exec 4>> parent.log
# child script
echo "Log me!" >&4
Related
I'm writing a shell script. what it does is it will create a file by the input that is received from the user. Now, i want to add the feature called "view a file" for my current script. Now, it's unreasonal to retype it again since i've already had a script that helps
I know it's crazy when it is possible to it with normal shell command. I'm actually writing a script that help me to create pages that are generated from the touch command. (this pages had attached date, author name, subjects, and title).
The question is how to call a another script or inhere another script?
Couple of ways to do this. My prefered way is by using source
You can -
Call your other script with the source command (alias is .) like this: source /path/to/script.
Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command.
Use the bash command to execute it: /bin/bash /path/to/script
I want to write an expect script which can do some task and in the end change the directory and give control to the user. I tried using
spawn cd path\to\dir
interact
but i am still in the same directory.
Any leads on how to accomplish this using expect ?
To change the directory in an expect script, you don't need to use the keyword "spawn" it works without it.
You may be a little confused, because in the terminal, in which you are executing the script, you will stay in the same directory, but the script changed the directory in which it is doing things.
I suppose you have spawned some command (like ssh, bash) and the spawned command is still running. Then at the end you can do like this:
send "cd /some/dir\r"
interact
Suppose I saw a process X in output of top or ps, I want to know which shell script started it, is there a easy way?
I think it's too stupid to find/grep the whole harddisk.
EDIT: I just want to find the script file name and path, not the command line which start the process.
In ps, there is the ability to view ppid, the parent process id. That will indicate the process that started the child. For a prettier display, check out pstree.
I realize that:
Dir.chdir("/path/to/some/dir/")
will temporarily change to the appropriate directory. My question is: is there any way to make this persistent? When I exist the script, I remain in the same directory as I started. I have also tried execute commands with backticks, but it seems like everything is run in a new shell.
Does Ruby have the ability to change my shell's directory?
No, it is not possible.
In fact, no child process can change the current working directory of its parent process.
When you execute a script (or any program) from your command shell you are actually doing a "fork/exec" pair, which means you create a "child process" which is separate from your shell "parent process" in many ways. The child can make changes to its own environment but cannot (typically) change the parent environment.
One small correction:
Dir.chdir("/path/to/some/dir/")
changes the directory for the rest of the script execution. A temporary change is possible with the block version of the command.
And to answer your question:
No, it is not possible.
Even the following script does not work:
puts Dir.pwd
puts `cd ..`
puts Dir.pwd
The cd-command in backticks starts a new environment, so your parent's shell will not change the directory.
I'm quite familiar with Dir.chdir("/xyz")
Unfortunately, this changes the directory of the process, but not actually the directory of the user. I'll make the following example to illustrate my need.
$~/: ruby my_script.rb
CHANGING TO PATH FOR USER NOT SCRIPT
$/Projects/Important/Path: pwd
$/Projects/Important/Path
See? I need the script to change the user's path. Performing system/backticks/Dir.chdir all adjust the process path, and end with the user sitting where they started, instead of the path I want them.
From what I've read exec was the way to go, since it takes over the existing process... but to no avail.
You can't, but you can do something which might be good enough. You can invoke another shell from ruby:
Dir.chdir("/xyz")
system("bash")
Running this will create a new bash process, which will start in the /xyz directory. The downside is that changing this process will bring you back to the ruby script, and assuming it ends right away - back to the bash process that started the ruby script.
Another hack that might work is to use the prompt as a hackish hook that will be called after each command. In the ruby script, you can write the new directory's path somewhere that can be read from both bash and ruby(for example a file - but not an environment variable!). In the PROMPT_COMMAND function, you check that file and cd to what's written there. Just make sure you delete that file, so you don't get automatically cded there after every command you run.