Best way to run a system command in Chicken Scheme - linux

I want to run following Linux command which opens the document with application registered with the system:
xdg-open mydocument.pdf
I see that many commands for this are available: run, run*, capture, shell, execute, system* etc.
from: http://wiki.call-cc.org/eggref/4/shell and https://wiki.call-cc.org/man/4/Unit%20utils
I also need the result of the command (ran successfully or not) from this.
Following (system without *), although not listed, also seem to work well:
(define result (system "xdg-open mydocument.pdf"))
Which of these will be the safest way to run system commands as above?

If the name of the document is hardcoded, it doesn't matter much what you use. If the file name is user-supplied, you must use qs to quote the arguments if you're relying on a string-based API like system's. It might be easier to pass arguments separately using process.
I don't know the shell egg very well, but if you want to get really fancy with shell calls, the scsh-process egg is a very nice alternative (full disclosure: I'm its author).

Related

how to provide the password automatically in lua script while sshing on the remote machine

Without using sshpass, sshkeys is there any way to do ssh to a remote machine and provide the password through the lua script only.And also to run a shell command in the background on remote machine after sshing.
How should i take password automatically after executing os.execute('ssh user#192.168.14.81')
That function is not supposed to be used like that. os.execute and io.popen are not really meant to be used at all, actually. They're barebones commands for when you need to do things that are not supposed to work this way but you really need to.
They're both based on standard C library and don't have much to offer. And ssh is handling its interaction with users in some not-so-standard way for security reasons. In general you'll have to use non-standard libraries, like luaposix, or attach some C++ libs for system interaction.
In relation to ssh, there is python library parallel-ssh. Older versions did their thing through ugly parsing of outputs, newer seem to reimplement the whole protocol. With enough desire, it'd be possible to make use of those with lua. Or just use python for the task.
Ass for peculiarities of lua interaction with processes, you may try this code (put it in a file as it'll clog stdin if you try pasting it in terminal)
f=io.popen([[
echo "this will show only on f:read";
echo "now here's your line";
read var; echo $var
]]);
print('this will print before bash command finishes, now type something');
f:write('This will be ignored completely')
print(f:read('*a'));
print('This will print after the bash command');
f:close()

Powerhsell/batch file script I can't quite get right

I've found two great tricks and I'd really like to get them to work together, but how to do so is totally eluding me.
One I just found is this: https://stackoverflow.com/a/2611394/12943 which is a batch file that starts powershell and feeds itself to powershell which is awesome.
the other trick is this one: https://stackoverflow.com/a/18739839/12943 Which lets PowerShell elevate itself if necessary (Feeding the script to itself once elevated)
I think it might work if the first line of the batch file that looks like this:
#findstr/v "^#f.*&" "%~f0"|powershell -&goto:eof
instead looked something like this:
#findstr/v "^#f.*&" "%~f0"|powershell "start-process powershell -verb runas" -&goto:eof
but that doesn't work and I can't quite get it right (I think the piping in that case goes to the original powershell process but not the one you runas).
The two methods obviously won't easily work together because they use similar mechanisms, but I can't help think that there is a clean solution that will both let me easily run a powershell script by typing the name into a command window AND self-elevate--both without any prior setup or extra software (since this is the first step of some system-configuration scripts on a system that cannot be attached to the internet).
I can (and will) do this by simply using the batch file solution and requiring the user to elevate manually but it seems like there should be an easier way.
Note that the batch file seems to also get around the executionpolicy which is somewhat curious but in this case preferred.
perhaps you should autoelevate the batch instead of your ps script. running powershell.exe from elevated cmd opens an elevate ps console. you can find auto elevating batch here https://stackoverflow.com/a/12264592/381149 .

How to track file creation and modification

We have put together a perl script that essentially looks at the argument that is being passed to it checks if is creating or modifying a file then it saves that in a mysql database so that it is easily accessible later. Here is the interesting part, how do I make this perl script run before all of the commands typed in the terminal. I need to make this dummy proof so people don't forget to run it.
Sorry I didn't formulate this question properly. What I want to do is prepend to each command such that each command will run like so "./run.pl ls" for example. That way I can track file changes if the command is mv or it creates an out file for example. The script pretty much takes care of that but I just don't know how to run it seamlessly to the user.
I am running ubuntu server with the bash terminal.
Thanks
If I understood correctly you need to execute a function before running every command, something similar to preexec and precmd in zsh.
Unfortunately bash doesn't have a native support for this but you can do it using DEBUG trap.
Here is a sample code applying this method.
This page also provide some useful information.
You can modify the ~/.bashrc file and launch your script there. Do note that each user would (and should) still have the privelege to modify this file, potentially removing the script invocation.
The /etc/bash.bashrc file is system-wide and only changeable by root.
These .bashrcs are executed when a new instance of bash is created (e.g. new terminal).
It is not the same as sh, the system shell, that is dash on Ubuntu systems.

Easier navigation through filesystem in linux shell (cli)

I often find myself taking a lot of time to navigate through my filesystem when using the linux shell. This generally occurs because the autocompletion of bash only works if you provide the start of the file/dirname. What I often end up in is a lot of 'ls' with 'grep' commands, finally doing a 'cd'.
When you use a GUI based filebrowser (like Nautilus) you can type any part of a file/dirname and it will have matches that it jumps to directly. This makes it a lot easier and faster to navigate.
I wonder if anybody knows any great tools that helps with this problem. I know of the existence of Midnight Commander, though I never really used it for real and I couldn't figure out a direct solution for my problem the first couple of times I tried it. Also it seems not suitable because I want to have my shell's current working directory to be changed so I can do stuff there, instead of being stuck in an external program like Midnight Commander.
Try autojmp
https://github.com/joelthelion/autojump
And following article provides another solution
http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html
You can first use the autocd or autopushd in zsh. You would just have to type the directory you want to go to, without the hassle of typing cd or pushd everytime.
You also have the globing possibility. For example, if I got those file in a directory:
1-a.tar
1-b.tar
c.tar
I can just type
*a.tar
without caring about the beginning of the file.
As a last solution you can always use an alias to the find command with a personalized option.

Show last command with up arrow on a linux c shell

I have implemented a simple linux shell in c. Now, I am adding some features and one I immediately thought about was to be able to show the last commands with the up arrow.
Question 1:
However, I have no idea how to accomplish this. Do you?
Question 2:
Any comment on how to store the "history" commands are also appreciated. I suppose something like a queue which allows access to all elements would be a good idea. Am I wrong? Do I have to implement it or is there already some good implementation out there I should know about?
Thanks.
Build libedit or readline support into your shell.
If you want to be lazy, you can use rlwrap:
rlwrap prog
I wrote the shell for HelenOS. Grab the bzr repo and navigate to uspace/app/bdsh (bdsh stands for the (b)rain (d)ead (sh)ell).
Other contributors have since added line editing / history / tab completion to the functions that handle input. Its written purely in ANSI C, does not link against glibc and implements its own functions. The code (both in the shell and underlying HelenOS libc) is 3 clause BSD, you can use it in anything.
If nothing else, it might help to just examine the implementation to get started.

Resources