0

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.

Guillaume
  • 21,685
  • 6
  • 63
  • 95
mckryall
  • 27
  • 1
  • 9

2 Answers2

1
#!/bin/sh

dir=/tmp
fnam=someshellscript

echo '#!/bin/sh' > $dir/$fnam
echo 'find /bin -name "*X*"' >> $dir/$fnam
chmod 755  $dir/$fnam
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • What do the lines "echo '#!/bin/sh' > $dir/$fnam" and "echo 'find /bin -name "*X*"' >> $dir/$fnam" do? – mckryall Mar 12 '14 at 22:30
  • 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. – Peter - Reinstate Monica Mar 12 '14 at 23:19
  • 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. – Peter - Reinstate Monica Mar 12 '14 at 23:23
  • 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. – mckryall May 01 '14 at 04:42
0
#!/bin/sh
echo "script goes here!" > /path/to/place
chmod 755 /path/to/place

?

a p
  • 3,098
  • 2
  • 24
  • 46