In my program I need to output to user which shell he is using. So in file /etc/udate-motd.d/00-header I wrote printf "$SHELL" but the problem is that even when I switching my shell to zsh, $SHELL is still equal to /bin/bash. I searched through the internet and found that I can bo it by using MyShell='ps -hp $$', and here is again a problem. When I use it MyShell is a string with number of processes (/etc/update-motd.d/00-header is also there) but there no word zsh.
So how can I understand which shell use the logging in person.
"the internet" gave you one kind of ps syntax. You've tagged this linux, so don't use BSD syntax. Try this:
ps hp $$ -o cmd
no dash
The users shell is determined in /etc/passwd. Why not take the information from there? You could
grep $USER /etc/passwd | cut -f7 -d:
to get the shell.
Related
I have a variable values stored in environment variables.
when I do
echo $myvar
I can see the value. Below is the example
~% echo $myvar
abcdefghijkl73
Now, I would like to read 8th/13th/14th character from this variable value and print the value
h73
Please help. Thanks in advance.
If you use BASH:
myvar="abcdefghijkl73"; echo ${myvar:7:1}${myvar:12:1}${myvar:13:1}
See "Parameter expansion" section in man bash
For the general POSIX shell you need to use external utility to do this:
echo $myvar | cut -c 8,13,14
This uses cut utility (more information about this utility you can find in info coreutils
I'm trying to change uppercase to lowercase using string replacement in bash, but i'm getting a bad substitution error.
> a=HEY
> echo $a
HEY
> echo ${a,,}
-bash: ${a,,}: bad substitution
# desired output is hey
I've seen similar questions to this, but in most cases it was down to using an earlier version of bash. I'm using GNU bash 4 and still having the same problems.
> bash --version
GNU bash, version 4.3.33(1)-release (x86_64-apple-darwin14.1.0)
This is a Mac thing maybe? Any help would be appreciated.
Looks like the bash that is first in PATH happens to be 4.3.33, but the bash you're running in the interactive session is probably an older version. Run echo "$BASH_VERSION" to check.
If the above is correct, run
type bash
to see the path of the newer version, probably something like /opt/local/bin/bash. I'll assume it is. If you want that to be your login shell, first add it to /etc/shells
sudo -e /etc/shells
After that, users are allowed to select that as their login shell by using the chsh (change shell) command
chsh -s /opt/local/bin/bash
Based on the comments on my comment, this is the answer:
echo $a | tr '[:upper:]' '[:lower:]'
users='awk '{print $1}' /etc/passwd | sort -u'
for user in $users
do
echo " - $user"
done
this is my shell script . Problem is that show's an error.
the error is ---> users: command not found
please give me the solution frinds
With the code the way it is now I see that you're not assigning the output of the awk|sort command to the variable (maybe you wanted to use ` instead of ' ?)
This works:
#!/bin/bash
users=$(awk '{print $1}' /etc/passwd | sort -u)
for user in $users
do
echo " - $user"
done
Although you should be aware that /etc/passwd is not separated by spaces, so awk '{print $1}' won't give you the user's name (which maybe is what you wanted)
Edit:
As per #Andy Lester's comment to your question: If you save this code in a file (let's say /tmp/myscript.bash) to run it you have to type in a terminal:
/bin/bash /tmp/myscript.bash
or, since it starts with #!/bin/bash (read here) you could make it executable (using chmod u+x /tmp/myscript.bash) and then call it, just typing /tmp/myscript.bash. You can also save it in one of the PATH directories (type echo $PATH to see which are they), make it executable and then you'll be able to call it from anywhere, but I don't really recommend doing that because you may end up overwriting juicy system's commands if you're not careful. For instance, let's say you call your script with the unfortunate name of ls, save it in the first directory of the $PATH (in my case, /usr/local/sbin) Every time you type ls, you won't be listing directories, but calling your script... Which is bad.
I am trying to execute the following unix command from within perl
`git log --pretty=format:%H | grep $id | wc -l`;
I keep getting an error saying
sh: -c : syntax error near unexpected token '|'
sh -c : '| wc -l'
How can I fix this error
The problem is that you have 123<NEWLINE> instead of 123 in $id, so you're passing the following to the shell:
git log --pretty=format:%H | grep 123
| wc -l
instead of
git log --pretty=format:%H | grep 123 | wc -l
Fix the value in $id. The best way is probably through chomp.
You'll probably have a more robust script if you implement some of this functionality using Perl subroutines instead of external Unix commands.
Using backquotes is good for a quick script, but it's inherently fragile, because you are exposed to the full power and weirdness of whatever Unix shell is configured for the user that is running your script (with wildcard expansion, input/output redirection, piping, etc.). This really applies to any programming language, and most languages (including perl) have safer ways to run external commands without using the shell. But one of the reasons Perl was created was to be able to combine under one programming language the text-processing abilities of a slew of Unix commands.
I'm new to bash scripting and I've been learning as I go with a small project I'm taking on. However, I've run into a problem that I cannot seem to get past.
I have a variable that I need to include in a command. When ran directly in the shell (with the variable manually typed), the command returns the expected result. However, I can't get it to work when using a variable.
So, if I manually run this, it correctly returns 0 or 1, depending if it is running or not.
ps -ef | grep -v grep | grep -c ProcessName
However, when I try to embed that into this while clause, it always evaluates to 0 because it's not searching for the correct text.
while [ `ps -ef | grep -v grep | grep -c {$1}` -ne 0 ]
do
sleep 5
done
Is there a way I can accomplish this? I've tried a myriad of different things to no avail. I also tried using the $() syntax for command substitution, but I had no luck with that either.
Thanks!
I think that instead of {$1} you mean "$1". Also, you can just do pgrep -c "$1" instead of the two pipes.
In addition, there's also no need to compare the output of grep -c with 0, since you can just see if the command failed or not. So, a much simplified version might be:
while pgrep "$1" > /dev/null
do
sleep 4
done
You should really use -C with ps rather than the messy pipes if you're using the full process name. If you're interested in substring matching, then your way is the only thing I can think of.