I am trying to use PS1 to customise my command prompt in mac, but the changes are not saved, when I try to re-open the terminal.
I have edited the .bashrc file, and the changes are effective only for that session. If I close and re-open the terminal, all saved changes are lost.
Below is the .bashrc file that i have edited.
export PS1="\u#\h \d \# \w >"
export PATH="/Users/avbanerj/test_script1:$PATH"
Could you please tell me what shall I add in my .bashrc or .profile so that changes are reflected everytime I open the terminal?
This is from info bash.
-Erik
6.2 Bash Startup Files
======================
This section describes how Bash executes its startup files. If any of
the files exist but cannot be read, Bash reports an error. Tildes are
expanded in filenames as described above under Tilde Expansion (*note
Tilde Expansion::).
Interactive shells are described in *note Interactive Shells::.
Invoked as an interactive login shell, or with '--login'
........................................................
When Bash is invoked as an interactive login shell, or as a
non-interactive shell with the '--login' option, it first reads and
executes commands from the file '/etc/profile', if that file exists.
After reading that file, it looks for '~/.bash_profile',
'~/.bash_login', and '~/.profile', in that order, and reads and executes
commands from the first one that exists and is readable. The
'--noprofile' option may be used when the shell is started to inhibit
this behavior.
When an interactive login shell exits, or a non-interactive login
shell executes the 'exit' builtin command, Bash reads and executes
commands from the file '~/.bash_logout', if it exists.
Invoked as an interactive non-login shell
.........................................
When an interactive shell that is not a login shell is started, Bash
reads and executes commands from '~/.bashrc', if that file exists. This
may be inhibited by using the '--norc' option. The '--rcfile FILE'
option will force Bash to read and execute commands from FILE instead of
'~/.bashrc'.
So, typically, your '~/.bash_profile' contains the line
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
after (or before) any login-specific initializations.
Related
I am trying to learn shell on Linux,
but I've got a problem which seems confusing.
My environment is:
OS: Manjaro 21.2.6 Qonos
Kernel: x86_64 Linux 5.15.38-1-MANJARO
Shell: zsh 5.8.1 (x86_64-pc-linux-gnu)
The problem is:
I created a file named foo, and echoed #\!/bin/sh to it, and the permission of file foo has been modified to 100 by using chmod.
The file foo doesn't have the read or write permission indeed, that's for true,
but when I executed the command ./foo, I got the error /bin/sh: ./foo: permission denied.
So why the Shell knows what the shebang in the file foo is without the read permission ???
If anyone of you can proide any suggesstions, I will be really thankful !
behavior-example
So why the Shell knows what the shebang in the file foo is without the read permission ???
It is not the shell that reads the shebang line but the OS/kernel.
A shell script can be executed in the same way as a compiled program. The process uses a function of the exec* family and passes ./foo as the program to execute. These functions are based on system calls.
The OS/kernel then detects if the file is a compiled program which can be executed directly or a script file which must be passed to an interpreter. If the file contains a shebang line, the OS will execute the specified interpreter, which does not have to be a shell, otherwise it will run the default shell. The script file is passed as an argument to the interpreter.
The shell is running with normal user permissions and will get an error when it tries to open the script file.
You can find some information about executiong scripts in the POSIX specification of the exec function family or in the Linux manual page for execve. Search for the word interpreter. You could check the Linux kernel source code for more details.
You get this error because the script itself do not have execution or read permission. To run script in shell (on your way) you need to have read and execute permissions:
chmod 500 foo
./foo
the other way to run the script is:
sh foo
and you do not need execution permission in this case. For the record the standard way is to use construction like:
#!/bin/sh
You do not need to escape exclamation mark
I am trying to create a file using the system command but it does not work for some reason. It just echoes the command back.
system("echo 'Hello2222, world.' >foo2.txt");
Hello2222, world. >foo2.txt
When I run the echo command in CMD, the file is created.
Notice that the documentation for system says
On Windows, system does not use a shell and there is a separate
function shell which passes command lines to a shell.
shell("echo 'Hello2222, world.' >foo2.txt")
will do what you want.
My Mac OS command line application is making Unix calls such as:
system("rm -rf /Users/stu/Developer/file);
perfectly successfully.
So why is the following not changing the current directory?
system("cd /Users/me/whatever");
system("pwd"); //cd has not changed
Because
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.
So each command is executed independently, each in a new instance of the shell.
So your first call spawns a new sh (with your current working directory), changes directories, and then exits. Then the second call spawns a new sh (again in your CWD).
See the man page for system().
The better solution is to not use system. It has some inherent flaws that can leave you open to security vulnerabilities. Instead of executing system() commands, you should use the equivalent POSIX C functions. Everything that you can do from the command-line, you can do with C functions (how do you think those utilities work?)
Instead of system("rm -rf ...") use this.
Instead of system("cd ...") use chdir().
Instead of system("pwd ...") use getcwd().
There are some differences, of course, but these are the fundamental equivalents of what you're trying to do.
Thanks in advance:
I'm attempting to use Notepad++ to log-in to a UNIX system. I am using the NPPExec console to do this, and the login process works... kind of. I use NPPExec to execute this script, named "sasunix.sh":
"C:\userblah\username\Desktop\plink.exe" -load "SUN4" -l myloginname -pw mypassword
As you can see I am using Putty's command line program "plink.exe" to send the command; "SUN4" is the session profile I am using. The problem is, the next screen logs me in (successfully), but proceeds to ask me for my password an additional time (which is part of the login procedure), followed by a request for my my terminal emulation setting (for me, this is 'xterm').
THE QUESTION: What additional lines would I add to my script to execute this sequence of inputs on the UNIX system (i.e. typing them in individually and pressing "ENTER" each time):
1.) thepassword 2.) xterm 3.) sas -nodms -nonews
I think this boils down to a misunderstanding of how commands are passed between NotePad++ and the NPPExec console window. At any point, if I press "F6", a prompt pops up, saying "WARNING: Console process still running...". This messagebox prompt allows me to type in a line which is then sent to the console... but how do I put a series of these inputs into the script?
You prob have this solved by now,
but i have done the following
a windows batch script that creates a file with unix commands, based on the a directory path
#ECHO OFF
:: store input variable
set str=%1
:: remove C:\ or Z:\ etc
set str=%str:~3%
:: replace \ with /
set str=%str:\=/%
:: append cd command
set str1=cd
set str=%str1% %str%
echo.%str%
:: make
echo make clean
echo make all
Install the NppExec plugin to Notepad++
Place the *.bat scripts and plink.exe in the Notepad++ program directory
Create the NppExec commands
// This line calls the bat file and creates a file tempcmd.sh with unix commands in it
"$(NPP_DIRECTORY)\unix_make_all.bat" $(CURRENT_DIRECTORY) >tempcmd.sh`
// This line connects to a remote machine and runs all the commands stored in tempcmd.sh
"$(NPP_DIRECTORY)\plink.exe" -ssh -2 -l username -pw password 192.168.000.000 -m "$(NPP_DIRECTORY)\tempcmd.sh"
Using the NppExec options i save the above commands and place a shortcut in the macros menu
I am executing a ksh script named abs.ksh located at /app/fao.... which connects to a server,
But the server is receiving a script named "ksh" which is present in /usr/bin...
I am not calling any script called ksh in abs.ksh(sorry cannot paste the code).
Also this happens only when the script is run in debug mode.
In non debug mode it works fine.
Can anyone give me a hint of what might be happening here.
In a standard "classic" Unix environment there may be multiple shells. E.g. 'sh' the original Bourne shell, 'ksh' - the Korn shell, csh - the C shell, bash, tcsh etc. etc.. A user login will have the default shell set per login.
The #! at the start of an executable script is an instruction to interpret & run the subsequent text with the name of the program following the '#!'.
E.g. run this with perl
#!/bin/perl
<.. perl stuff ..>
So yes #!/usr/bin/ksh - will run the script with the command interpreter (shell) at that location.
Need more info. wrt how you run in debug mode. I.e. are you typing 'ksh -x ...' or 'sh -x' - if so where is that on your search path. E.g. 'whence ksh' - maybe you're running with a different shell in debug mode.
Also which os is this ?