Running sudo on user-defined functions in fish - fish

» cat ~/.config/fish/config.fish
function take
command mkdir $argv;and cd $argv
end
function check
sudo dmesg -c>/dev/null;
make clean; make;
/usr/local/bin/kedr start $argv;
sudo insmod "$argv.ko"; sudo rmmod $argv;
/usr/local/bin/kedr stop
dmesg;
end
function sudo
if functions -q $argv[1]
set argv fish -c "$argv"
end
command sudo $argv
end
While running I get this error:
» sudo check "simple-no-macro"
fish: Unknown command 'check simple-no-macro'
fish:
check simple-no-macro
^

You've asked this on GitHub as well, so here's my answer from there:
The problem here is that the function you've defined isn't present in the new instance of fish you start.
You'd be better off defining the check function in a file saved in ~/.config/fish/functions/check.fish, which will then let the function work across instances.
Side note: bash does let you export functions across instances using environment variables, but both zsh and ksh use a similar method to fish - see Propagating shell functions from Unix Power Tools.

Related

How can I simply redirect text containing $HOME into /etc/zshenv with sudo?

I want a script to set up a better (but non-default) location for my .zshrc file:
% sudo echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv
but this fails:
zsh: permission denied: /etc/zshenv
The problem is related to the redirection being done first, but there is no obvious way to solve this. I can hear you saying 'use zsh -c' but that fails:
sudo zsh -c 'echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv'
results in $HOME being evaluated inside the shell launched with sudo, so my /etc/zshenv file contains
ZDOTDIR=/var/root/.config/zsh
Trust me: my home directory is not /var/root.
I tried --preserve-env but that did not work:
sudo --preserve-env zsh -c 'echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv'
Obviously I can find a workaround for this, such as saving $HOME to a local variable in the script, but it seems like there must be a more direct and simple and straightforward way to do it.
I just realized I was thinking about the problem all wrong. I want to prevent $HOME from being evaluated. Of course escaping a single quote inside a single quote is a new kind of PITA, but here it is:
sudo zsh -c 'echo '"'"'ZDOTDIR=$HOME/.config/zsh'"'"' >> /etc/zshenv'

How to log the output along with error messages to a file while running a script on psql command line on Freebsd OS?

On RHEL, the below command works:
psql -h hostname -U username -p port_no -d database -f /tmp/myfile.sql &> logfile01.txt
On FreeBSD, this throws error:
"Invalid null command"
Please suggest.
If you use this only on the command line then there is no need to change the shell.
To redirect stdout and stderr to a file in C-Shell synthax simply use ">& filename".
Different story is, if you want to write shell scripts. Bourne Shell and it's clones (like i.e. Bash) are better suited for writing script. See this Unix FAQ "Csh Programming Considered Harmful": http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
This redirection works in bash
&> logfile01.txt
, but it does not work in csh which is the default shell in FreeBSD.
# set | grep shell
shell /bin/csh
# ls -la &> logfile01.txt
Invalid null command.
Bash is not installed by default. You can install it
pkg install bash
and configure it as the default shell.

Why is "-H" command not found?

I have a strange error with linux somehow interpreting sudo -H as two separate commands.
I'm on Cent OS 7, and I get the following:
/var/tmp/<random string>: line 8: -H: command not found
This is very vexing to me. Why would it not know this uption of sudo?
My guess would be that you have an alias or bash function that is suppressing your call to sudo. Try running the command with the full pathname for sudo (/usr/bin/sudo) on both systems, and type type sudo to see if there is an alias or bash function that is being called instead of the executable.
If there is, check the usual places like ~/.bashrc for where it is being defined so you can remove it.
Alternatively, it could be unrelated to sudo, and instead be related to whatever script you are calling with sudo.

PostgreSQL command psql not found, trouble adding to $PATH

I am trying to add PostgreSQL to my $PATH variable. I tried this to see where psql is
whereis psql
To which I got a null response. I do have the PostgreSQL.app installed so I clicked on "Open psql" from the GUI and it prompted the Terminal to open and display:
/Applications/Postgres.app/Contents/MacOS/bin/psql; exit;
So I tried to add the above to the $PATH variable in my ~/.bash_profile (which may have its own problems since I don't know if you can add paths with .app extensions to the $PATH) but when I restart my Terminal and do echo $PATH | grep Postgres.app I get nothin'.
Here's an approach to take help isolate problems you may have.
Step 1: See if you can add PostgreSQL to your PATH without using Bash dot files.
$ export PATH=/Applications/Postgres.app/Contents/MacOS/bin:$PATH;
$ which psql
If this works...
Step 2: Verify that ~\.bash_profile is being sourced when a new user session is started.
Add the following to the end of your ~/.bash_profile:
echo "From bash_profile.";
Now restart Terminal.app or iTerm and see if that message appears about your prompt.
If this works...
Step 3: Add PATH to ~/.bash_profile.
In ~/.bash_profile:
export PATH=/Applications/Postgres.app/Contents/MacOS/bin:$PATH;
Restart your terminal and run:
$ which psql
If you're not seeing:
/Applications/Postgres.app/Contents/MacOS/bin/psql
Then it might be time to scrap trying to install PostgreSQL as a Mac package and use Homebrew.
NOTE: It's psql and NOT pgsql.
From the Postgres documentation page:
sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
restart your terminal and you will have it in your path.

Running last command with sudo in fish only works if it has no arguments?

I'm trying to write a function that does the equivalent of sudo !! in Bash. It works, but only when the last command has no arguments.
So far the function is:
function s --description "Run last command (or specified command) using sudo"
if test $argv
switch $argv[1]
case '!!'
command sudo (echo $history[1])
case '*'
command sudo $argv
end
else
command sudo fish
end
end
Testing the relevant line:
$ command sudo whoami
root
$ whoami
nick
$ command sudo (echo $history[1])
root
So far so good, now lets try a command with a few args:
$ echo hi >> /etc/motd
An error occurred while redirecting file '/etc/motd'
open: Permission denied
$ command sudo (echo $history[1])
sudo: echo hi >> /etc/motd: command not found
Hmm, strange.
Got it working using eval.
function sudo --description 'Run command using sudo (use !! for last command)'
if test (count $argv) -gt 0
switch $argv[1]
case '!!'
if test (count $argv) -gt 1
set cmd "command sudo $history[1] $argv[2..-1]"
else
set cmd "command sudo $history[1]"
end
case '*'
set cmd "command sudo $argv"
end
else
set cmd "command sudo fish"
end
eval $cmd
end
I had the same problem as you, and I fixed it by using oh-my-fish
(it's a plugin manager for fish shell) https://github.com/oh-my-fish/oh-my-fish. You can install it with this command :
curl -L https://get.oh-my.fish | fish
Then install the plugin bang-bang (to allow !! and !$) with this command :
omf install bang-bang