How to exclude error message from bash string - linux

I have command in my xubuntu:
nvidia-settings -q gpucoretemp | grep '(user-xubuntu:0.0):' | sed 's/^.*: //'
And I have the result:
libEGL warning: DRI2: failed to authenticate
46.
If I try to exclude 'libEGL warning: DRI2: failed to authenticate' :
nvidia-settings -q gpucoretemp | grep -v 'libEGL warning: DRI2: failed to authenticate' | grep '(user-xubuntu:0.0):' | sed 's/^.*: //'
The result is the same. How to ignore the 'libEGL warning: DRI2: failed to authenticate' string?

You can first redirect stderr to stdout and then filter the result with grep.
nvidia-settings -q gpucoretemp 2>&1 | grep -v 'libEGL warning: DRI2: failed to authenticate' | grep '(user-xubuntu:0.0):' | sed 's/^.*: //'

Related

sed on mac not updating the file as expected

I have a users.txt file with the following content:
[builders.ca]
UniqueID=DB#LqlFP
Login=buildca
Pass=5nFvZLwx
RelativePath=1
[DeluxeDoors.ca]
UniqueID=RgOkvU4Z
Login=DeluxDSM
Pass=L9pP3iaK
RelativePath=1
[Sonicwall.com]
UniqueID=JVpFoXad
Login=firewall
Pass=azasadsa
RelativePath=1
I wrote a script to replace all the passwords with random passwords in the file.
The script is:
users=($(cat users.txt | grep 'Login=' | cut -c 7-))
for user in "${users[#]}"; do
pass=$(cat users.txt | grep -A 2 $user | grep 'Pass' | cut -c 6-)
new_pass=$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-zA-Z0-9' | head -c 8)
echo $pass;
echo $new_pass;
#perl -pi -e 's/$pass/$new_pass/g' users.txt
sed -i '' 's/"${pass}"/"${new_pass}"/g' users.txt
done
But it is not updating the passwords in the users.txt.
Any help would be highly appreciated.
sed -i '' 's/'"${pass}/${new_pass}"'/g' users.txt
^ ^
These seem to be missing, so the sed was getting ${pass} and ${new_pass} as literal strings, not the expanded variables.

how to get loaded kernel module file position

command :
lsmod
loaded kernel module list
command :
modinfo `lsmod | awk {'print $1'}` | awk {'print $2'}
loaded kernel module file position
I want list
KernelModule | file position
ex :
xfs | /lib/modules/3.10.0-514.el7.x86_64/kernel/fs/xfs/xfs.ko
how to get example list Command?
Try this:
lsmod | grep -v ^Module | awk '{print $1}' | xargs -n 1 -I MOD sh -c "echo -n MOD; echo -n ' | '; modinfo MOD | grep filename | awk '{print \$2}'"

Save variable with command substitution... command not found

I need to save into a bash variable a string after a grep and sed treatment.
Here my code :
echo ${plan} | grep -e '^\S' -e 'Home directory:' | sed -e 's/Home directory: //'
/home/james
That's what I need to save into a variable... so I tried :
HOME_DIRECTORY=$(${plan} | $(grep -e '^\S' -e 'Home directory:') | $(sed -e 's/Home directory: //'))
and
HOME_DIRECTORY=`${plan} | grep -e '^\S' -e 'Home directory:' | sed -e 's/Home directory: //'`
But both give me :
line 121: Home: command not found
Change you command to ,
HOME_DIRECTORY=$(echo ${plan} | grep -e '^\S' -e 'Home directory:' | sed -e 's/Home directory: //')
That is, you need to include the whole command inside $().

Shell command with operator followed by pipes

I want a one line command to grab the most recent restart date from JBoss restart logs. I'm currently using the following, which works
ls -rt $JBOSS_HOME/log/ser* |
xargs grep -ih "Incomplete\s*Deploy" |
awk '{print $1" "$2}' | tail -n 1
Now, I want to add an echo if the grep fails to match anything, i.e.:
ls -rt $JBOSS_HOME/log/ser* |
xargs grep -ih "Incomplete\s*Deploy" ||
echo 'NO RESTART' |
awk '{print $1" "$2}' |
tail -n 1
The problem here is that it seems that the OR operator now causes anything following to go with the failed case. How do I specify that I want to OR (echo) to be printed only when the grep fails? When the grep succeeds it should work like the first command I posted.
Use { to group the terms:
ls -rt $JBOSS_HOME/log/ser* |
xargs grep -ih "Incomplete\s*Deploy" && {
awk '{print $1" "$2}' |
tail -n 1
} ||
echo 'NO RESTART'
or as one line:
ls -rt $JBOSS_HOME/log/ser* | xargs grep -ih "Incomplete\s*Deploy" && { awk '{print $1" "$2}' | tail -n 1; } || echo 'NO RESTART'
No additional subshell required.
You can group commands within a pipe by using parentheses:
ls -rt $JBOSS_HOME/log/ser* | ( xargs grep -ih "Incomplete\s*Deploy" || echo 'NO RESTART' ) | awk '{print $1" "$2}' | tail -n 1
This makes the commands in the parentheses run in a subshell, which from the standpoint of the pipe, makes them work as a single command, i.e. they'll take the input and either print the grep match or 'NO RESTART', and then that will be fed into awk.
res=$(ls -rt $JBOSS_HOME/log/ser* | xargs grep -ih "Incomplete\s*Deploy" | awk '{print $1" "$2}' | tail -n 1)
echo "${res:-NO RESTART}"
Also note that this is not safe for filenames with spaces/etc.

Pipe to command that does not accept piping

I want to use MPC (CLI interface to MPD) but unfortunately to me it does not accept piping.
So something like:
ll | grep "pattern" | sed 's/this/that/' | mpc
does not work for me, nor
ll | grep "pattern" | sed 's/this/that/' | mpc -
This
MPCTMP=`ll | grep "pattern" | sed 's/this/that/'` && echo $MPCTMP
works as expected, but this:
MPCTMP=`ll | grep "pattern" | sed 's/this/that/'` && mpc $MPCTMP
does not return results, variable is not understood but this program
I'm new to Linux and could not find anything searching with Google
Thanks
Try xargs
ll | grep "pattern" | sed 's/this/that/' | xargs mpc

Resources