1

I've been creating mac shell executables with this method:

  • Create a file;
  • Add #!/bin/sh;
  • Add the script;
  • Run chmod 755 nameofscript.

I now need to create a shell script to create a shell script in another directory and make it executable (with chmod) so that it can be run on startup.

3
  • 5
    And what's the question? Commented Mar 12, 2014 at 22:20
  • Don't assume /bin/sh is bash. And this very simple to do, but feels a lot like homework, can you post what you've tried? Commented Mar 12, 2014 at 22:58
  • if you only want the script to be executable without altering the read write permission already defined you can use this command: chmod +x nameofscript. Commented Mar 13, 2014 at 1:00

2 Answers 2

2
#!/bin/sh

dir=/tmp
fnam=someshellscript

echo '#!/bin/sh' > $dir/$fnam
echo 'find /bin -name "*X*"' >> $dir/$fnam
chmod 755  $dir/$fnam
Sign up to request clarification or add additional context in comments.

4 Comments

What do the lines "echo '#!/bin/sh' > $dir/$fnam" and "echo 'find /bin -name "X"' >> $dir/$fnam" do?
Ok, let's dissect them: "echo" is a command which just writes all its arguments to the standard output, here '#!/bin/sh' (the single quotes are for the shell to indicate the limits of the string and will be eliminated). The echo command ends with the special char ">" which indicates an redirection of the standard output. The standard output goes to the file whith a name indicated by the string to the right of it which is constructed from $dir/$fnam by replacing $dir with /tmp and $fnam with someshellscript, resulting (note the slash) in /tmp/someshellscript.
For find I recommend "man find" although googling "unix find examples" may be more helpful in the beginning; find is not intuitive. The >> operator is similar to the > operator above in that it redirects the standard output to a file; but it appends instead of creating a new one (like > would do) which is what we want to do here: Write into our shell script, line by line. The chmod in the end should look famiiliar ;-). I think what you were missing was the simple "echo" in conjunction with output redirection. The rest is just like the commands you enter manually on the terminal.
Thanks for the advice, but I see now that I didn't even need to make a new script. Apologies for necromancy. While this may be very well-documented, "a p"'s answer is shorter and less confusing to a beginner.
1
#!/bin/sh
echo "script goes here!" > /path/to/place
chmod 755 /path/to/place

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.