Scripting

acamare5
New Contributor

Any youtube channels or website where I can practice and learn scripting or get practice scripts to run and see how they work.

 

Thanks

4 REPLIES 4

sdagley
Esteemed Contributor II

@acamare5 You'll find lots of scripts posted on Jamf Nation, and this post asking for scripting resources has recommendations for a few sites: https://community.jamf.com/t5/jamf-pro/scripting-resources/td-p/220869 (I'll 2nd the recommendation there for Scripting OS X and the site author has also written a couple of scripting related books you'll find mentioned on the site)

Jamf's own @talkingmoose has an intro to scripting video, and the accompanying article has an extensive list of resources: https://www.jamf.com/blog/just-10-commands-then-keep-learning/

It hasn't been updated since 2014, but Apple's Developer site does have a Shell Scripting Primer: https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/Intro...

 

obi-k
Valued Contributor III

@talkingmoose scripting video helped me out a lot. I re-watch it here and there. Easy to follow and very useful.

 

Jaykrishna1
Contributor II

Yes, there are several YouTube channels and websites that can help you learn scripting and practice with scripts. Here are a few popular options:

  1. YouTube Channels:
  • Derek Banas
  • Chris Hawkes
  • Corey Schafer
  • Traversy Media
  1. Websites:
  • Codecademy (codecademy.com)
  • FreeCodeCamp (freecodecamp.org)
  • Udemy (udemy.com)
  • Coursera (coursera.org)

These resources offer interactive tutorials, video lessons, and practice exercises to help you learn scripting and improve your skills. Additionally, you can find a wide range of scripts on websites such as GitHub (github.com) and Stack Overflow (stackoverflow.com) that you can run and modify to better understand how they work.

AJPinto
Honored Contributor III

Honestly, I have never found any youtube channels for macOS scripting to be very useful. They all go from this is terminal to "now that we are all experts on looping if statement you are ready to go" way too fast. Granted we all learn a bit differently, but I always find it best to have a practical real world goal.

 

I suggest starting with file manipulation. 

  • How to create a file
    • touch command
  • How to move a file
    • mv command
  • How to make a directory
    • MkDir command
  • How to change permissions on a file
    • ChMod command
  • How to change owner ship of a file
    • ChOwn command
  • How to delete a file
    • rm command
  • How to delete a directory
    • rm -rf command (with the -rf argument)
  • How to print to a file
    • text or command to print > or >> path to file where we want the results

From this you can test each command really easy, and then start stringing things together for a script. With it being file related its easy to see the results of what you are doing. 

Make a file on your desktop.

 

touch /Users/{your user name/Desktop/file_name.txt

 

 

*do not use ~ for your profile in a script, play with it and figure out why this wont work if run from JAMF.

 

touch ~/Desktop/File_Name.txt

 

 

Move the file from your desktop to documents

 

mv /Users/{your user name/Desktop}/file_name.txt /Users/{your user name/documents

 

 

Make a command to create a file on your desktop, then move it to your documents. Then add when your mac rebooted last to the text document

*Scripts need the 1st line to point to the interpreter, 99% of the time that will be #!/bin/sh. however it can be any interpreter on the Mac.

 

#!/bin/sh

touch /users/{user name}/desktop/file.txt
mv /users/{user name}/desktop/file.txt /users/{user name}/documents
uptime >> /users/{user name}/documents/file.txt

 

 

 

A script is literally just a text file, the file extension for a shell script is .sh but macOS really does not need the file extension. Use something like Visual Studio code to make your scripts, you want a plain text editor so done use text edit or MS Word. If you got a few bucks to toss around I like Code Runner 2. XCode is really overkill but can also be a text editor for scrips. 

To run a script locally open terminal and use sudo sh {path to script}

 

sudo sh "/Users/desktop/my amazing script.sh"

 

 

The last thing I will mention on scripting, terminal sees spaces as new commands. If your file path has spaces in it, quote the file path or or terminate the spaces. The three examples below will all terminate the space between File and Name.

 

/Users/{user name}/desktop/"file name.txt"
"/Users/{user name}/desktop/file name.txt"
/Users/{user name}/desktop/file\ name.txt

 

 

Basically just about all scripting is file manipulation in some way shape or form. Learning the basics is a really good place to start. You want to read or modify a plist, you use defaults read or defaults write but its still just a text file.