Ask the compiler to ignore #pragma message - objective-c

As said in the title, I want the compiler to ignore pragma message for the time being, so it's easier for me to read and fix actual warnings. I've done some searching, but there doesn't seem to be any information on it.

No it isn't possible, so the best thing to do would be to mass-edit all the #pragmas out:
$ cd MySourceFolder
$ find . -name \*.m -exec perl -p -i -n -e 's/^#pragma/\/\/#pragma/' {} \;
When you want the #pragma's back again:
$ cd MySourceFolder
$ find . -name \*.m -exec perl -p -i -n -e 's/^\/\/#pragma/#pragma/' {} \;
If you do this kind of thing alot, I would wrap that in a script, and put it into your ~/bin directory.

Related

find: missing argument to `-exec' in SSH command

I'm using SSH inside a CI/CD Pipeline (so it's non-interactive), and trying to execute a couple find command (among others) to change the ownership of files and directories after executing LFTP mirror, but I keep getting this error (which makes the whole Pipeline fail):
find: missing argument to `-exec'
This is the command that uses find:
ssh -i ~/.ssh/id_rsa $USERNAME#$HOST "[other commands...]; find $SOME_PATH/ -type d -exec 'chmod 755 {} \;' && find $SOME_PATH/ -type f -exec 'chmod 644 {} \;' && echo Done"
I've already tried using escaped double quotes like so: -exec \"chmod 755 {} \;\" - but keeps throwing the same error.
What would be the main issue here?
EDIT: Solved. I removed any quotes for the -exec, removed the && and append an extra semicolon ; to each find and it works as expected.
ssh -i ~/.ssh/id_rsa $USERNAME#$HOST "[other commands...]; find $SOME_PATH/ -type d -exec chmod 755 {} \;; find $SOME_PATH/ -type f -exec chmod 644 {} \;; echo Done"
So use -exec whatever-command {} \;; [other command, echo, find, ls, whatever...].
Please check this answer for more information: https://unix.stackexchange.com/a/139800/291364
[...] When find sees that spurious exit after the -exec … ; directive, it doesn't know what to do with it; it hazards a (wrong) guess that you meant it to be a path to traverse. You need a command separator: put another ; after \; (with or without a space before). [...]
\; is processed to ; locally before the string is passed to the remote shell. You need to escape the backslash so the the ; remains escaped on the remote end.
ssh -i ~/.ssh/id_rsa $USERNAME#$HOST \
"[other commands...]; find $SOME_PATH/ -type d -exec 'chmod 755 {} \\;'
&& find $SOME_PATH/ -type f -exec 'chmod 644 {} \\;' && echo Done"
A better idea would be to use single quotes for the command argument and pass the value of $SOME_PATH as an argument to a shell.
ssh -i ~/.ssh/id_rsa $USERNAME#$HOST \
sh -c '...;
find "$1" -type d -exec chmod 755 {} \; &&
find "$1" -type f -exec chmod 644 {} \; &&
echo Done' _ "$SOME_PATH"
Note that chmod and its arguments each need to be separate arguments to the find.
In fact, you don't need to run find twice; you can provide two -exec primaries, each paired to a different -type:
ssh -i ~/.ssh/id_rsa $USERNAME#$HOST \
sh -c '...;
find "$1" \( -type d -exec chmod 755 {} \; \) -o
\( -type f -exec chmod 644 {} \; \)
&& echo Done' _ "$SOME_PATH"
Rather than the complex find commands (and associated quoting/escaping mess), you can use the built-in capabilities of chmod sympolic modes to set the permissions on files and directores differently. Specifically, the "X" permission means essentially "execute if it makes sense", which mostly means directories rather than files. The main exception is that if there's a file that already has execute set, it assumes it's intentional and keeps it. If that's ok, you can use this simpler command:
chmod -R u=rwX,go=rX "$1" # Set non-executable files to 644, executables and directories to 755
If you need to specifically clear execute bits on files, or just want to stick with find, another option take advantage of the fact that chmod accepts multiple arguments to use find ... -exec ... {} + instead of the \; version. "+" isn't a shell metacharacter, so it doesn't reqire special treatment:
find $SOME_PATH/ -type d -exec chmod 755 {} + ; find $SOME_PATH/ -type f -exec chmod 644 {} +

How to locate code in PHP inside a directory and edit it

I've been having problems with multiple hidden infected PHP files which are encrypted (ClamAV can't see them) in my server.
I would like to know how can you run an SSH command that can search all the infected files and edit them.
Up until now I have located them by the file contents like this:
find /home/***/public_html/ -exec grep -l '$tnawdjmoxr' {} \;
Note: $tnawdjmoxr is a piece of the code
How do you locate and remove this code inside all PHP files in the directory /public_html/?
You can add xargs and sed:
find /home/***/public_html/ -exec grep -l '$tnawdjmoxr' {} \; | xargs -d '\n' -n 100 sed -i 's|\$tnawdjmoxr||g' --
You may also use sed immediately than using grep -but- it can alter the modification time of that file and may also give some unexpected modifications like perhaps some line endings, etc.
-d '\n' makes it sure that every argument is read line by line. It's helpful if filenames has spaces on it.
-n 100 limits the number of files that sed would process in one instance.
-- makes sed recognize filenames starting with a dash. It's also commendable that grep would have it: grep -l -e '$tnawdjmoxr' -- {} \;
File searching may be faster with grep -F.
sed -i enables inline editing.
Besides using xargs it would also be possible to use Bash:
find /home/***/public_html/ -exec grep -l '$tnawdjmoxr' {} \; | while IFS= read -r FILE; do sed -i 's|\$tnawdjmoxr||g' -- "$FILE"; done
while IFS= read -r FILE; do sed -i 's|\$tnawdjmoxr||g' -- "$FILE"; done < <(exec find /home/***/public_html/ -exec grep -l '$tnawdjmoxr' {} \;)
readarray -t FILES < <(exec find /home/***/public_html/ -exec grep -l '$tnawdjmoxr' {} \;)
sed -i 's|\$tnawdjmoxr||g' -- "${FILES[#]}"

how to grep for a pattern in a few set of files under a directory?

i have a large number of files under a directory. out of that i need to search for a pattern ONLY in files which were created/last modified on the month of November. By using Awk and xargs command i was able to acheive this. But I would like to know whether there is a simple grep command to acheive the same. Below is the command which I used
ls -ltr |grep "Nov"|awk '{print $9}'|xargs grep -i (pattern)
Could you please help me on this one ?
Use the find command:
find . -newermt 2013-11-1 ! -newermt 2013-11-30 -type f -print0 | xargs -0 grep -i pattern

ssh on remote server to kill process, pushd script folder and execute the script is not wor

I have a requirement to kill remote process of specific patter, pushd the path to startup and execute the script.
I tried so far with
pid=$(ssh -q username#virt ps -ef|grep $APP|grep $PORT|awk '{print $2}')
ssh -q username#virt kill -9 $pid
ssh -q username#virt "find /shared/local/path1/app -name "start_app*" -exec grep -nl "9122" {} \;| xargs -0 -I '{}' bash -c 'pushd $(dirname {});bash {};'"
When I execute above command kill processing is working fine. The final step to find for scriptfile and execute script by pushing the folder to the path is not working.
For some reason the pushd is not working fine.
The command on the local server do work fie with
find /shared/local/path1/app -name "start_app*" -exec grep -nl "9122" {} \;| xarg -0 -I '{}' bash -c 'pushd $(dirname {});bash {};'
Please help a more effective solution to accomplish this task.
You have an error in the quotes here:
ssh -q username#virt "find /shared/local/path1/app -name "start_app*" -exec grep -nl "9122" {} \;| xargs -0 -I '{}' bash -c 'pushd $(dirname {});bash {};'"
Try this in stead:
ssh -q username#virt "find /shared/local/path1/app -name 'start_app*' -exec grep -nl '9122' {} \;| xargs -0 -I '{}' bash -c 'pushd \$(dirname {});bash {};'"

Know how many lines of code you have in an Xcode project?

I've got quite a big project and eventually I finished it. I'm just curious to know how many lines of code there are altogether in my project. I'm using Xcode 3.
So can you actually find out how many lines of code have been compiled?
Open up Terminal.app, go into your project's root directory, and run this command:
For Swift only:
find . \( -iname \*.swift \) -exec wc -l '{}' \+
For Obj-C only:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) -exec wc -l '{}' \+
For Obj-C + Swift:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -exec wc -l '{}' \+
For Obj-C + Swift + C + C++:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h -o -iname \*.hh -o -iname \*.hpp -o -iname \*.cpp -o -iname \*.swift \) -exec wc -l '{}' \+
Terminal quick tips:
ls: list directory contents
cd: change directory
Press tab to autocomplete
Remember to put "\" backslash before spaces
I suggest going one folder down from the main project so you get rid of code count from the frameworks
Open up Terminal.app, cd into your project's root directory, and run this command:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h \) -exec wc -l '{}' \+
If you other file types you also want to include in your count, then add more -o \*.ext clauses.
You can use sloccount or cloc to do this, they are both compatible with Objective-C code.
I recommend using sloccount, you can get a nice HTML report if you also use Jenkins. The HTML report will enable you to drill down to the different directories and files.
This is a command line for just having an overview of your code, if you are in the root dir of your Xcode project:
sloccount --duplicates --wide YOUR-TARGET-NAME
And if you want to generate a report to use in Jenkins, just add the --details flag:
sloccount --duplicates --wide --details YOUR-TARGET-NAME > build/sloccount.sc
and install the Jenkins plugin for sloccount via Jenkins UI.
You will be able to see examples of such reports in Jenkins in this blog article (disclaimer: I am the author): http://blog.octo.com/en/jenkins-quality-dashboard-ios-development/#step1-1.
One way is to load a copy into Xcode and use "Project Analyzer for Xcode 4". Search for "Xcode" in the Apple Mac App Store. I have not used this program but I happened to see it yesterday when I was searching for Xcode related apps in the Mac App Store.
Hope that helps.
A bash script which finds line count of files WITHOUT COMMENTS AND EMPTY LINES. The script doesn't count comments which start with //. Copy the script to your project folder and run by sh scriptname.sh.
For swift change \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) to \( -iname \*.swift \)
# $excluded is a regex for paths to exclude from line counting
excluded="some_dir_name\|some_dir_name2\|lib\|thisfile"
countLines(){
# $total is the total lines of code counted
total=0
# -mindepth exclues the current directory (".")
for file in `find . -mindepth 1 \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) | grep -v "$excluded"`; do
# First sed: only count lines of code that are not commented with //
# Second sed: don't count blank lines
# $numLines is the lines of code
numLines=`cat $file | sed '/\/\//d' | sed '/^\s*$/d' | wc -l`
# To exclude only blank lines and count comment lines, uncomment this:
#numLines=`cat $file | sed '/^\s*$/d' | wc -l`
total=$(($total + $numLines))
echo " " $numLines $file
done
echo " " $total in total
}
countLines
I'm not sure about any tools that plug into Xcode directly (why are you still using Xcode 3 when 4.1 is freely available on Lion?), but I find that the command-line cloc tool works well with Objective-C code.
A really nice unix command is xargs. See the "pipe to xargs below". For example:
find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) | xargs wc -l
Oddly, I'll have to figure out though why this answer comes out a tiny bit lower for me than the answer from #Esqarrouth