What would 2>&1 do in command line - command-line

In the line find /. -group bandit 2>&1 | grep -v "Permission denied"
what would the operators > and & actually be doing. I've found various pages explaining their uses. I'm not sure what > would do if it's not pointing to a file. Is & making the whole command run in the background, seeing as it's close to the end?

Related

Eclipse Oxygen: Reading input from a file with <, Run configuration

So in the terminal, I can run my program with
./letter --stack -b ship -e shot -c --output W < input.txt > output.txt
How do I achieve the same functionality in Eclipse? I've figured out how to do --stack -b ship -e shot -c --output W by going to the arguments tab under Run configuration.
I've looked online and seen a lot of posts about using the Common tab to input and output redirection but I can't get it working. When I check only the input file, it doesn't work and the Debug perspective shows it's stuck somewhere trying to read from a stream. When I check allocate console and input file, then it gets stuck on user input (aka I can manually type stuff) in the console.
So how do I get < input.txt and by extension > output.txt working?

Msys: how to keep STDERR on the screen and at the same time copy both STDOUT and STDERR to files

I would like to be able to
Display STDERR on the screen
Copy STDOUT and STDERR in files (and if possible in the same file)
For information, I am using Msys to do that.
.
After doing some research on the SO, I have tried to use something like
<my command> > >(tee stdout.log) 2> >(tee stderr.log)
Bu I got the following error:
sh: syntax error near unexpected token `>'
.
Any idea on how to do that?
There might be no direct solution in Msys but the >(tee ... ) solution works fine in *Nix, OSX, and probably Cygwin.
The workaround is to grep all the errors and warnings we want to keep them on the screen.
I have successfully used the following command for a makefile to compile C code:
make 2>&1 | tee make.log | grep -E "(([Ee]rror|warning|make):|In function|undefined)"
I have a simple script (test.sh) that generates STDOUT and STDERR:
#!/bin/bash
echo hello
rm something
exit
Then, to do what you want execute with the following:
./test.sh > stdout.log 2> >(tee stderr.log >&2)
You'll get the STDERR on the screen, and two separated log files with STDERR and STDOUT. I used part of the answer given here
Note that I am assuming you don't have a file called something on the current directory :)
If you want both STDOUT and STDERR to go to the same file, use the -a option on tee:
./test.sh > std.log 2> >(tee -a std.log >&2)

Perl using find in qx

III am writing a Perl script that will need to SSH out to numerous remote servers to perform some gzipping of log files. In the following line, I keep receiving this error and am struggling to determine what's causing this. The error I'm getting is;
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `cd /appdata/log/cdmbl/logs/; echo cd /appdata/log/cdmbl/logs/; find . -type f ( -iname '*' ! -iname '*.gz' ) -mmin +1440 ;; exit 0'
And of course, as you can tell by the error, the line I am trying to write is;
my $id = qx{ssh -q $cur_host "cd $log_path; echo cd $log_path; find . -type f \( -iname '*' ! -iname '*.gz' \) -mmin +1440 \;; exit 0"};
Am I overlooking something here that is causing the unexpected token '(' issue I am
receiving?
NOTE: I removed the -exec from find just so I could see if I can get past this issue first.
Thanks.
You need to backslash the parentheses for the shell. Using single backslash in double quotes is not enough, Perl removes the backslash. Use double backslash \\(.
This is probably not going to answer your question, but it's a nice alternative that I would like to propose.
You said you cannot install additional modules on the production servers. You need to run a bunch of stuff where you are looking for files and zipping them. That can all be done in Perl, and you may have more controll over it than through the "doing command line stuff from a Perl script" approach.
Take a look at Object::Remote, which was written for exactly that purpose. It lets you ssh into machines and run Perl stuff there that you have installed on your local machine. That way, you do not need to add modules or install anything on the remote. All it needs is any kind of more or less recent Perl, which fortunately almost every Linux comes with.
There is a very good lightning talk about it by the author Matt Trout that is well worth watching.
If the command you built results in a syntax error, wouldn't the first step be to see what command you built?
print qq{ssh -q $cur_host "cd $log_path; echo cd $log_path; find . -type f \( -iname '*' ! -iname '*.gz' \) -mmin +1440 \;; exit 0"}, "\n";

Error when starting terminal: "-bash: QSTK/local.sh: No such file or directory"

"-bash: QSTK/local.sh: No such file or directory"
I've been getting this error for over a year. Any idea how I'd go about fixing it? None of the similar suggestions on SO solve this problem.
In response to Olivier's suggestion:
When I run
grep QSTK/local\.sh' /etc/* ~/.??*
I get this output:
grep: /etc/aliases.db: Permission denied
grep: /etc/kcpassword: Permission denied
grep: /etc/krb5.keytab: Permission denied
grep: /etc/master.passwd: Permission denied
grep: /etc/sudoers: Permission denied
/Users/DylanRichards/.bash_profile:source QSTK/local.sh
/Users/DylanRichards/.bash_profile.pysave:source QSTK/local.sh
/Users/DylanRichards/.profile:source QSTK/local.sh
Dylan-Richardss-MacBook-Pro:~ DylanRichards$
When I type
-type f -size -200 -print0 2>/dev/null | xargs -0 grep 'QSTK/local\.sh' /dev/null
It returns nothing. The error is still occurring at the start of my terminal.
following your answers to my questions:
to look for the script responsible, first in the shell's startup scripts:
grep 'QSTK/local\.sh' /etc/* ~/.??*
and if it is not enough:
find / -type f -size -200 -print0 2>/dev/null | xargs -0 grep 'QSTK/local\.sh' /dev/null
(just copy/paste this, while loggued as root in a terminal window, so that you can grep most files)
(the additional "/dev/null" is there to force grep to display the matching file's name even if there is only 1 parameter, which cuold happen if xargs split in a way that make the last file alone)
(I only check files of less than around 100kb [100 x 512 bytes], as anything bigger is most probably a binary and not a script...)
Note that this could fail if "QSTK" is in a variable, or is contructed...
But it could work. Try it and let us know if it's enough for you to correct the mistake, or provide additionnal informations if you don't know how to proceed.
=== update =====================
after you posted:
/Users/DylanRichards/.bash_profile:source QSTK/local.sh
/Users/DylanRichards/.bash_profile.pysave:source QSTK/local.sh
/Users/DylanRichards/.profile:source QSTK/local.sh
I'd recommend you
1) check that you have, indeed, a /Users/DylanRichards/QSTK/local.sh file.
ls -l /Users/DylanRichards/QSTK/local.sh
2) check its content and ensure you are OK to execute it each time you log-on
more /Users/DylanRichards/QSTK/local.sh
3) if it is OK to execute it (ie, if it's really something you really need/want to execute at each login) then
chmod +x /Users/DylanRichards/QSTK/local.sh
ls -al /Users/DylanRichards/QSTK/local.sh
You can, if you want, post the info of the ls -l /Users/DylanRichards/QSTK/local.sh
But as for its content, please verify it (do not post it here if it could contain important security information...). (If however you feel it's safe to show it to us, you can post the : cat /Users/DylanRichards/QSTK/local.sh (unless it's more than 20 lines...)
Finally, in case you do NOT find the file in /Users/DylanRichards/QSTK/local.sh :
find / -type f -name "local.sh" -print 2>/dev/null | grep QSTK/local.sh
will show you where it is. Once again, check its content, and if it seems fine (and needed) edit the ~/.bash_profile and ~/.profile with the correct path to the correct file.
open .bashrc file using a text editor as follows
$gedit .bashrc
In the text file look for the path .QSTK/local.sh probably in the last line you may something similar to
VIRTUAL_ENV_DISABLE_PROMPT=1 source /home/QSTK/local.sh
comment out the line adding a # in the beginning of the line and then save
Try it out

Assistance with the "find" and "grep" command

I'm looking for help with a one-liner that I can run from the Mac OS X terminal. I use MAMP for web development on my Mac. I have a lot of CakePHP projects in my "/Applications/MAMP/htdocs" directory. For the sake of simplicity, let's just say that I had two CakePHP projects and that this was the output of the find /Applications/MAMP/htdocs -type d -iname Controller* command:
/Applications/MAMP/htdocs/my_cake1.3_project/app/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/app/tests/cases/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/console/templates/skel/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/console/templates/skel/tests/cases/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/libs/controller
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/cases/libs/controller
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/test_app/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/test_app/plugins/test_plugin/controllers
/Applications/MAMP/htdocs/my_cake2_project/app/Controller
/Applications/MAMP/htdocs/my_cake2_project/app/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Console/Templates/skel/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Console/Templates/skel/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/test_app/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/test_app/Plugin/TestPlugin/Controller
Now, sometimes I want to find a piece of code that I know I used in one of my CakePHP projects' controllers, but I can't remember which project it was, so I want to search all of them. I don't want to waste time searching in the "app/tests/cases/controllers" folder or any of the ones within "cake/", though. The find /Applications/MAMP/htdocs -type d -iname Controller* | grep -i /app/Controller command gives me the list of folders I want to search in:
/Applications/MAMP/htdocs/my_cake1.3_project/app/controllers
/Applications/MAMP/htdocs/my_cake2_project/app/Controller
I just need to find a way to take that output, add a slash and asterisk (/*) to the end of each line, and pipe each line to the grep -il "string to search for" command. Any help would be appreciated. Thanks!
solution 1
maybe you want to check two options of find command: (i)path and regex
with them you could narrow your find result and pass found files to your grep -il "searchString" for example by |xargs . it looks like:
find /Applications/MAMP/htdocs -type f -ipath "*/app/Controller/*.php"
| xargs grep -il 'foo'
with -regex would be more flexiable.
solution 2
however if you really really want to :
find a way to take that output, add a slash and asterisk (/*) to the
end of each line, and pipe each line to the grep -il "string to search
for" command.
(btw, here "pipe" won't work.)
you could do this:
find .(your original find).. |grep -i "/app/Controller"
|sed -r 's#^(.*)$#grep -il "foo" \1/*#g'|sh
the trick was done by the sed....|sh. the sed line will pick the result of your previous grep, add grep command and options :(grep -il "foo") and append "/*" in order to construct a complete grep command. finally pipe to sh, to execute it.
Have you tried this?
find /Applications/MAMP/htdocs -type d -iname Controller*
-exec grep -il "string to search for" {} /;