Linux ssh output of two commands in an input - linux

I am looking for a command that will let me look for an rpm installed in my machine and remove it, the only information about the rpm to delete that I have is a suffix of its name, that's what I want to do in one command:
rpm -qa | grep -i $rpmnameSuffix >> output
rpm -e output

Use command substitution to substitute the output of a command into another command line.
rpm -e "$(rpm -qa | grep -i $rpmnameSuffix)"

Related

cant execute command stored in txt file in bash successfully

I have to make ssh request for different nodes based on given IP.
different ssh commands are stored in node.txt as below
0.0.0.0 a ssh -t user#0.0.0.0 sudo -u node0 /path/script.sh
0.0.0.1 b ssh -t user#0.0.0.1 sudo -u node1 /path/script.sh
0.0.0.2 c ssh -t user#0.0.0.2 sudo -u node2 /path/script.sh
I try to grep the needed ssh command like this
comm=$(grep 0.0.0.2 node.txt | grep c | cut -f3)
when I run
status=$($comm)
the following error appears:
/path/script.sh not found
while if I hard coded the command in the script itself it work correctly,
comm='ssh -t user#0.0.0.2 sudo -u node2 /path/script.sh'
status=$($comm)
what could be the problem here?
#Karim Ater
Try the following at your system:
    comm=$(grep 0.0.0.2 node.txt | grep c | cut -f3)
    $ echo $comm
Hence I tried
    comm=$(grep 0.0.0.2 node.txt | grep c | sed "s/.*ssh/ssh/;")
    $ echo $comm
I cannot confirm the contents of node.txt to know related delimiter.
Hence I used sed instead of using cut.

Trying to use dpkg within a folder only on files with the keyword 'mono' in the title

I'm currently trying to install mono using dpkg, and all the other files within the same folder using apt-get, I know I need to use some form of this:
sudo grep 'mono' | dpkg -R --install >/dev/null
however there are too many unknowns for me to complete it and fill in whatever blanks there may be, any help would be greatly appreciated!
Try this:
ls | grep "mono" | sudo xargs dpkg -R --install >/dev/null
The ls will only give files from the current directory. You could also use ls -d *mono* instead of the ls and grep, but I think the ls and grep is easier to understand
The grep is as you had, but now has input from the ls to grep on. You can try ls | grep "mono" to see what files it selects.
Then the sudo is moved to the dpkg part of the script to make dpkg run as root. The way you had it grep runs as root and dpkg as your user
The xargs will take whatever input you had, and put it after the next command. It will take command line length limits in account and execute multiple dpkg commands if the command line gets too big. Note that if your files have spaces in their names, xargs will see the space as start of a new file and you will have problems. There are solutions to that, but really, the easiest solution is to have no files with spaces.
In this example, lets say there are 2 files from the grep "mono1.deb and "mono2.deb" the command executed will be dpkg -R --install mono1.deb mono2.deb. If for some reason you want only one deb per dpkg execution you can change it to ...xargs -n1 dpkg... and it will run dpkg -R --install mono1.deb and also dpkg -R --install mono2.deb
The >/dev/null make sure you won't get any output. Note that you will still get the errors though!

Yocto: bitbake command to regenerate all RPM files

I wanted to make some free space and deleted all directories in build/tmp/deploy/rpm/, thinking yocto would detect it and recreate them at the next bitbake call... it was a mistake ! :(
Here's the bitbake error just in case:
bitbake <image_name>
[...]
ERROR: ... do_rootfs: minicom not found in the base feeds (<image_name> corei7-64-intel-common corei7-64 core2-64 x86_64 noarch any all).
[...list of every package...]
Is there any way to force the regeneration of every rpms using bitbake ?
Forcing the regeneration with bitbake -f -c package_write_rpm <package> works, but I didn't find the command to do it all at once.
I tried cleaning the state of the native rpm packages thinking it might detect it and invalidate the rpm files states, but no luck:
bitbake -f -c cleanall nativesdk-rpm nativesdk-rpmresolve rpmresolve-native rpm-native
bitbake <image_name>
I also thought this would work, but it didn't:
bitbake -f -c package_write_rpm <image_name>
I will try to hack something with bitbake-layers show-recipes and xargs, but it would be cool to have a proper bitbake command.
I am using Yocto 2.1 (Krogoth).
Thanks !
I ended up doing the following script and use bitbake dependency tree to get the list of packages (thanks to this yocto/bitbake reference page):
# bitbake -g <image> && cat pn-depends.dot | grep -v -e '-native' | grep -v digraph | grep -v -e '-image' | awk '{print $1}' | sort | uniq | grep -v "}" | grep -v cross | grep -v gcc | grep -v glibc > packages-list.txt
# cat packages-list.txt | xargs bitbake -f -c package_write_rpm
Maybe there is a more straightforward solution ? For now this worked.

grep and tee to identify errors during installation

In order to identify if my installation has errors that I should notice, I am using grep command on the file and write the file using tee because I need to elevate permissions.
sudo grep -inw ${LOGFOLDER}/$1.log -e "failed" | sudo tee -a ${LOGFOLDER}/$1.errors.log
sudo grep -inw ${LOGFOLDER}/$1.log -e "error" | sudo tee -a ${LOGFOLDER}/$1.errors.log
The thing is that the file is created even if the grep didn't find anything.
Is there any way I can create the file only if the grep found a match ?
Thanks
You may replace tee with awk, it won't create file if there is nothing to write to it:
... | sudo awk "{print; print \$0 >> \"errors.log\";}"
But such feature of awk is rarely used. I'd rather remove empty error file if nothing is found:
test -s error.log || rm -f error.log
And, by the way, you may grep for multiple words simultaneously:
grep -E 'failed|error' ...

Command wont run in script

I am trying to run a command in a shell script but it is not working.
Out side of the script in the shell I can run the following command on the needed host. The file is created with the correct information inside.
sudo cat /etc/shadow | cut -d: -f1,8 | sed /:$/d > /tmp/expirelist.txt
When the command is run in my script I first ssh over then run the command but I get the following error.
[batch#testserver01 bin]$ checkP.sh
Testserver02
/usr/local/bin/checkP.sh: line 7: /tmp/expirelist.txt: Permission denied
Here is a part of the script. I have tried using ssh -o
#!/bin/bash
for SERVER in `cat /admin/lists/testlist`
do
echo $SERVER
ssh $SERVER sudo cat /etc/shadow | cut -d: -f1,8 | sed /:$/d > /tmp/expirelist.txt
...
What is causing the Permission denied error?
Don't use hardcoded temporary filenames -- when you do, it means that if one user (say, your development account) already ran this script and left a file named /tmp/expirelist.txt behind, no other user can run the same script.
tempfile=$(mktemp -t expirelist.XXXXXX)
ssh "$SERVER" sudo cat /etc/shadow | cut -d: -f1,8 | sed /:$/d >"$tempfile"
By using mktemp, you guarantee that each invocation will use a new, distinct, and previously-nonexisting temporary file, preventing any chance of conflict.
By the way -- if you want the file to be created on the remote system rather than the local system, you'd want to do this instead:
ssh "$SERVER" <<'EOF'
tempfile=$(mktemp -t expirelist.XXXXXX)
sudo cat /etc/shadow | cut -d: -f1,8 | sed /:$/d >"$tempfile"
EOF
I'm not sure about this, but you could be running into an issue with having the 'sudo' within your script. You could try removing the 'sudo' from the script, and running it like this:
$ sudo checkP.sh

Resources