I am looking to automate my xcode projects. It all works fine except the projects name with spaces. I have tried the following commands:
output_apps=`find ./ -name "*.app" -print`
output_apps=`find ./ -name "*.app"`
When I run
find ./ -name "*.app" -print
without storing into variable, it gives me output as expected as mentioned below:
.//Ten EU.app
.//Ten Official App EU.app
.//Ten Official App.app
.//Ten.app
However when I store the output of above command in a variable as below
output_apps=`find ./ -name "*.app" -print`
and then run the following for loop for get the names
for curr_app in $o
do
echo "$curr_app"
done
It shows
.//Ten
EU.app
.//Ten
Official
App
EU.app
.//Ten
Official
App.app
.//Ten.app
How do I maintain the spaces between each output and get the following output?
Ten EU.app
Ten Official App EU.app
Ten Official App.app
Ten.app
${var/ /} removes the first space character. ${var// /} removes all space characters. There's no way to trim just leading and trailing whitespace with only this construct.
In bash, $(<any_shell_cmd>) helps to run a command and capture the output. Passing this to IFS with \n as delimiter helps to convert that to an array. Benjamin W. This will get only the first file of the results of find into the array.
Symbol: $# The symbol $# is used to retrieve the length or the number of arguments passed via the command line. When the symbol $@ or simply $1, $2, etc., is used, we ask for command-line input and store their values in a variable.
To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]
If you don't need to store the file names in a variable, you can use find -print0
in combination with xargs -0
. This separates the found entries by NUL bytes instead of newlines. xargs
reads these NUL separated values and calls some command with as many arguments as possible.
find ./ -name "*.app" -print0 | xargs -0 some-command
If you want, you can limit the number of arguments given to some-command
with xargs -n 1
find ./ -name "*.app" -print0 | xargs -0 -n 1 some-command
Yet another approach is to read the files with a while
loop
find ./ -name "*.app" -print | while read f; do
some-command "$f"
done
This calls some command with one file at a time. The important point is to enclose the $f
into double quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With