Shell alias expansion under ESXi host does not auto complete - AND - detecting if running interactively - sh

Since ESXi does not come with bash, I am sourcing an .sh file to set up some custom aliases for common commands while connected via ssh.
On other distros like RHEL, I can type part of an alias and hit tab to autocomplete. This does not seem to work under ESXi 7.x.
Is there a switch or something that I can turn on to make autocomplete work for custom aliases, or is this just a limitation of the shell that ESX offers?
NOTE: If type l<tab> then I DO get the built in commands that start with L.
Also while I'm on the topic of the ESXi shell… On RHEL I have this line in my .bashrc file
[[ $- != *i* ]] && return
Purpose of this code being that if the current session is not being ran interactively then return else run rest of code.
When I run this on esx I get the error sh: *i*: unknown operand. Does the shell not support this substring methodology?
If I run echo $- then I get “smi” as the output.
Thanks

As you probably know ESXi provides ash (NOT bash) and Busybox. Although Busybox includes ash as I recall ESXi uses a custom built executable (check where /bin/ash points or doesn't point to). The Wikipedia article on ash gives a good overview of ash and its minimalist philosopy. Shell history was originally not included and autocompletion is definitely regarded as a nice to have that didn't make the cut. Sorry to be the bearer of bad news.

Related

What is the difference in using ! and % before a python command? [duplicate]

This question already has an answer here:
In Jupyter Notebooks on Google Colab, what's the difference between using % and ! to run a shell command?
(1 answer)
Closed 1 year ago.
I know that using ! before a command in python runs the command using the terminal.
For example:
!ls
!unzip zippedfile.zip
However, I noticed that using % also works for some bash commands but not for others.
So
%ls
will work, but
%unzip
will NOT work.
What is the difference between these two prefixes?
The ! escape runs an external shell command, like ping -c 3 www.google.com, not a Python command. Python or ipython has no idea what ping does, it just passes over control to it, and displays its output.
The % escape runs an ipython built-in command or extension, i.e. something that ipython itself understands.
To quote the documentation,
User-extensible ‘magic’ commands. A set of commands prefixed with % or %% is available for controlling IPython itself and provides directory control, namespace information and many aliases to common system shell commands.
The source of confusion here is probably that e.g. ls is also available as a "magic" command for portability and convenience. (It's portable in that it works even on platforms where there is no system ls command, like Windows.)

chsh: Cannot determine your user name. while trying to setup zsh

I have installed oh-my-zsh and now trying to make it my default shell. but when I trying to enter chsh -s $(which zsh) command it is giving me this --> chsh: Cannot determine your user name. error.
Please help me, guys.
It's probable that your system restricts the ability to change the shell.
You should contact your own uni tech support for instructions, or construct a dedicated shell rc file that runs zsh directly, depending on the $SHELL you have by default.
Here's a sample code which I had to use in my system.
if($?prompt) then
exec /usr/bin/zsh -l
export SHELL=/usr/bin/zsh
endif

How to make command line tool work in windows and linux?

Making my PHP Command line application support Linux and Windows. Currently it has this code below to work from command line on Linux/unix
How can I make it work on Windows? I lan on having a setting to determine if the sytem is Linux or Windows and using the correct commands based on that but I do not know how to make these function below work in Windows
exec() is a PHP function to run stuff through the command line
exec("rm -f $dest_file", $var);
exec("mv $quant_file {$this->tmp_path}/{$src_filename}-quant.png");
You could test which platform you're on using the PHP_OS constant and run commands accordingly.
I would, however, suggest that you use the PHP provided filesystem functions (if possible).
Here are some links:
http://www.php.net/manual/en/ref.filesystem.php
http://www.php.net/manual/en/ref.dir.php

How can I find out what script, program, or shell executed my Perl script?

How would I determine what script, program, or shell executed my Perl script?
Example: I might want to have human readable output if executed from shell (customized for each type of shell), a different type of output if called as a script from another perl script, and a machine readable format if executed from a program such as a continuous integration server.
Motivation: I have a tool that changes its output based on which shell executes it. I'd normally implement this behavior as an option to the script, but this tool's design doesn't allow for options. Other shells have environment variables that indicate what shell is running. I'm working on a patch to support Powershell, which has no such special variable.
Edit: Many of these answers happen to be linux specific. Unfortuantely, Powershell is for Windows. getppid, the $ENV{SHELL} variable, and shelling out to ps won't help in this case. This script needs to run cross-platform.
You use getppid(). Take this snippet in child.pl:
my $ppid = getppid();
system("ps --no-headers $ppid");
If you run it from the command line, system will show bash or similar (among other things). Execute it with system("perl child.pl"); in another script, e.g. parent.pl, and you will see that perl parent.pl executed it.
To capture just the name of the process with arguments (thanks to ikegami for the correct ps syntax):
my $ppid = getppid();
my $ps = `ps --no-headers -o cmd $ppid`;
chomp $ps;
EDIT: An alternative to this approach, might be to create soft links to your script, make the different contexts use different links to access your script and inspect $0 to build logic around that.
I would suggest a different approach to accomplish your goal. Instead of guessing at the context, make it more explicit. Each use case is wholly separate, so have three different interfaces.
A function which can be called inside a Perl program. This would likely return a Perl data structure. This is far easier, faster and more reliable than parsing script output. It would also serve as the basis for the scripts.
A script which outputs for the current shell. It can look at $ENV{SHELL} to discover what shell is running. For bonus points, provide a switch to explicitly override.
A script which can be called inside a non-Perl program, such as your continuous integration server, and issue machine readable output. XML and/or JSON or whatever.
2 and 3 would be just thin wrappers to format the data coming out of 1.
Each is tailored to fit its specific need. Each will work without heuristics. Each will be far simpler than trying to guess the context and what the user wants.
If you can't separate 2 and 3, have the continuous integration server set an environment variable and look for it.
Depending on your environment, you may be able to pick it up from the environment variables. Consider the following code:
/usr/bin/perl -MData::Dumper -e 'print Dumper(\%ENV);' | grep sh
On my Ubuntu system, it gets me:
'SHELL' => '/bin/bash',
So I guess that says I'm running perl from a bash shell. If you use something else, the SHELL variable may give you a hint.
But let's say you know you're in bash, but perl is run from a subshell. Then try:
/bin/sh -c "/usr/bin/perl -MData::Dumper -e 'print Dumper(\%ENV);'" | grep sh
You will find:
'_' => '/bin/sh',
'SHELL' => '/bin/bash',
So the shell is still bash, but bash has a variable $_ which also show the absolute filename of the shell or script being executed, which may also give a valuable hint. Similarily, for other environments there will most probably be clues left in the perl %ENV hash that should give you valuable hints.
If you're running PowerShell 2.0 or above (most likely), you can infer the shell as a parent process by examining the environment variable %psmodulepath%. By default, it points to the system modules under %windir%\system32\windowspowershell\v1.0\modules; this is what you would see if you examine the variable from cmd.exe.
However, when PowerShell starts up, it prepends the user's default module search path to this environment variable which looks like: %userprofile%\documents\windowspowershell\modules. This is inherited by child processes. So, your logic would be to test if %psmodulepath% starts with %userprofile% to detect powershell 2.0 or higher. This won't work in PowerShell 1.0 because it does not support modules.
This is on Windows XP with PowerShell v2.0, so take it with a grain of salt.
In a cmd.exe shell, I get:
PSModulePath=C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
whereas in the PowerShell console window, I get:
PSModulePath=E:\Home\user\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsP
owerShell\v1.0\Modules\
where E:\Home\user is where my "My Documents" folder is. So, one heuristic may be to check if PSModulePath contains a user dependent path.
In addition, in a console window, I get:
!::=::\
in the environment. From the PowerShell ISE, I get:
!::=::\
!C:=C:\Documents and Settings\user

How do I get the "Command Buffer" in Solaris 10?

When working on a linx CShell u get the option to press the up / down arrows to select the last command/s typed or the Command Buffer. This even works on Windows.
However this is not functional when working on Solaris, to which i recently switched. I am guessing that the shell is also a CShell.
Please tell me what key combination is required to have this feature on Solaris ?
The default shell in Solaris has command history, but you can also use Bash instead, it's more user friendly. Just type 'bash' (no quotes) at the command line. You can also edit /etc/passwd to make bash your default shell.
The "official" default shell for Solaris is actually sh, the original Bourne shell (see Chapter 10 of the Advanced User Guide for Solaris for more info). If you'd like to change it to csh or tcsh—and you're not root (it's generally considered bad practice to use anything but sh as root's default)—just issue passwd -e /path/to/shell_of_your_choice <loginname>. I'm guessing this would probably look like passwd -e /bin/csh <loginname>, but you'd probably want to make sure it exists, first.
It may be that it's the Korn shell in which case try <ESC>k.
bash at least will allow you to switch modes with "set -o vi" or "set -o emacs".
Maybe you can use the !! command, to repeat the previous one.
Use "echo $SHELL" to see what your login shell is. If it's ksh or bash, try "set -o emacs". If that works, you'll be able to use ^P to go back a command. ^R lets you search for a command, ^F and ^B to move around within the command.
If you can´t change your default shell, or you just want to try out one that works, you can kick off any other shell from your command line. I recommend you tcsh, which will have good command line editing and history using the arrow keys. Type /bin/tcsh at your prompt to try it out. You can use the earlier responses to change your default shell if you like tcsh. Make sure your have the following in your $HOME/.cshrc file:
set filec
set history=1000 # or some other large number
set autologout=0 # if you are logging in remotely under your account.
I hope this helps.
You enable history temporarily if you use BASH by typing
HISTSIZE=1000
which will enable up and down keys and store 1000 commands. After termal disconnetion all history will be gone.
This works on solaris 10.
For permanent solution add these lines to ~/.bashrc
HISTSIZE=1000
HISTFILESIZE=1000