LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash ,to work under MAC OS (https://www.linuxquestions.org/questions/linux-software-2/bash-to-work-under-mac-os-4175449395/)

nuser 02-10-2013 04:08 AM

Bash ,to work under MAC OS
 
Hello, can anyone help by showing how the script should look to work under Mac OS!
Code:

#!/bin/bash

USR=$(logname)
f=`find /home/$USR/proba -name "*.doc" -o -name "*.xsl" -o -name "*.pdf"`

for file in $f
do
  chattr +i $f
done

I want to achieve the same result!

TB0ne 02-10-2013 10:48 AM

Quote:

Originally Posted by nuser (Post 4888132)
Hello, can anyone help by showing how the script should look to work under Mac OS!
Code:

#!/bin/bash
USR=$(logname)
f=`find /home/$USR/proba -name "*.doc" -o -name "*.xsl" -o -name "*.pdf"`

for file in $f
do
  chattr +i $f
done

I want to achieve the same result!

Ok...so what happens when you run this on a Mac?? And you do realize that Mac OS is NOT Linux, right? So unless you have all the same utilities (that also take the same command-line attributes), the script won't work. Same with path names, etc.

If you want to know about Mac shell scripting, Apple has a scripting tutorial...have you read it?
http://developer.apple.com/library/m...roduction.html

nuser 02-10-2013 11:04 AM

Quote:

Originally Posted by TB0ne (Post 4888510)
Ok...so what happens when you run this on a Mac?? And you do realize that Mac OS is NOT Linux, right? So unless you have all the same utilities (that also take the same command-line attributes), the script won't work. Same with path names, etc.

If you want to know about Mac shell scripting, Apple has a scripting tutorial...have you read it?
http://developer.apple.com/library/m...roduction.html

I wrote this script myself, and I know the differences but I have not Mac on hand to experiment. I was hoping someone who has experience with Mac to help me because there is no way to check the result. To put it short I have no choice but to write someone for me. If you have no experience with this, do not write in vain. I wrote this script for 2 minutes if you know how and you have two minutes write/type the right commands!Please!

TB0ne 02-10-2013 01:59 PM

Quote:

Originally Posted by nuser (Post 4888518)
I wrote this script myself, and I know the differences but I have not Mac on hand to experiment. I was hoping someone who has experience with Mac to help me because there is no way to check the result. To put it short I have no choice but to write someone for me. If you have no experience with this, do not write in vain. I wrote this script for 2 minutes if you know how and you have two minutes write/type the right commands!Please!

There is no way anyone here can know what knowledge you have...we know what you post. You asked for assistance, and you were given a link and advice that could help you. Now you say that you know the differences (implying that you are experienced in Mac scripting).

If you want someone to do work for you, then you need to pay them. Otherwise, you need to do the work yourself...this is true for ANYTHING, from writing a script, to painting a house. Otherwise, you can find a friend with a Mac, or buy one, and do it yourself. There are even options for you to get Mac OS running in a virtual machine, which you can find on the Internet. Those are three options for you to do it yourself; if you want someone to do it for you, post the job in the LQ Job Marketplace.

And if you're going to reply rudely when people offer you help, you probably won't get people offering to help you, much less do work for you.

sundialsvcs 02-10-2013 02:03 PM

"Mac OS" (as in System-9) is not Unix-based. OS/X, however, is.

OS/X is based on the Mach (BSD Unix) kernel, not Linux, and although many of the commands are similar, they are not entirely.

nuser 02-11-2013 12:17 AM

Quote:

Originally Posted by TB0ne (Post 4888603)
There is no way anyone here can know what knowledge you have...we know what you post. You asked for assistance, and you were given a link and advice that could help you. Now you say that you know the differences (implying that you are experienced in Mac scripting).

If you want someone to do work for you, then you need to pay them. Otherwise, you need to do the work yourself...this is true for ANYTHING, from writing a script, to painting a house. Otherwise, you can find a friend with a Mac, or buy one, and do it yourself. There are even options for you to get Mac OS running in a virtual machine, which you can find on the Internet. Those are three options for you to do it yourself; if you want someone to do it for you, post the job in the LQ Job Marketplace.

And if you're going to reply rudely when people offer you help, you probably won't get people offering to help you, much less do work for you.

Excuse me if I sound rude!

David the H. 02-11-2013 12:10 PM

Code:

#!/bin/bash

USR=$(logname)
f=`find /home/$USR/proba -name "*.doc" -o -name "*.xsl" -o -name "*.pdf"`

for file in $f
do
  chattr +i $f
done

Well, your first problem is you're using "chattr +i $f" instead of "chattr +i $file".

But more importantly, you've fallen for the incredibly common Don't Read Lines With For error. This is compounded by your attempt to store the output of find inside a single, scalar variable.

Finally, you need to learn how to properly quote variables, a vital concept in scripting.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


Instead of for, use a while+read loop, preferably with -print0 null separators (be sure your version of find supports it).

Code:

while IFS='' read -rd '' fname; do

    chattr +i $fname"

done < <( find "/home/$USR/proba" \( -name "*.doc" -o -name "*.xsl" -o -name "*.pdf" \) -print0 )

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

And here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html


PS: $(..) is highly recommended over `..`

nuser 02-11-2013 01:37 PM

@David the H. Thanks for the good advices!


All times are GMT -5. The time now is 12:54 PM.