Hi, everyone! This is my first question on this site.
Hello and Welcome! Nice to have you here :)
A script is a program which automates a task. It usually is executed without compiling (read transforming) it to a binary format (1s and 0s which you cannot read; a script will be a text file). That's why scripts are written for an interpreter(some software which reads the text and does what the text says) rather than an operating system or specific Kernel. Examples for scripting languages include:
.cmd/.bat) or Powershell (.ps1) on Windows, or Bash or Kornshell or ZSH or ... on *NIX)You can make one by opening a text editor (for example Notepad on Windows) and writing in the language of your choice. All you need to execute it is a matching interpreter. You usually have a Shell Script interpreter on your computer, so I will show you examples for that.
Example in CMD (just write the following code into a file and name it hello_world.cmd or something like that and double-click it):
@echo off
echo.
echo Hello World!
echo.
PAUSE
Or bash (file name hello_world, optionally hello_world.sh, and make it executable with chmod +x hello_world)
#!/bin/bash
printf "\nHello World!\n"
For other languages, you usually need to download the interpreter. For example for JavaScript, you will need Node.JS if you intend to execute it without browser.
For more information and good tutorials, you should use google and go to a site, like CodeCademy, which teaches the language you are interested in.
What do you mean exactly? a script? a bash script? php script? node script? javascript script? what is your finality?
Tommy Hodgins
CSS & Element Queries
A “script” is a series of instructions written in a programming language that a machine can read and understand.Once you have written a series of instructions, you can then ‘run’ the script and have the machine perform the operations in the order you specified.
Think of a program like a cookbook and a script like a recipe inside that cookbook. You can have a variety of ingredients you are working with (these could be arguments, variables, and data) and then you use commands in your programming language to manipulate and perform operations on this input (these are the numbered steps in your recipe) and at the end of the script you could have some kind of output (like the finished recipe).
If you want to write your first script today, right here in your web browser, try following these instructions:
Open 'Inspect Element' in your browser by right-clicking, or from your browser's menu. This should open a new window with code inside.
Select the 'Console' or 'JavaScript Console' tab inside your 'Inspect Element' window. You should see a prompt where you can enter code that looks like this:
To write your first few scripts, let's try a few things! First, type
a-l-e-r-t-(-'-h-e-l-l-o-'-)into the console and press Enter.alert('hello')If you're writing this into the JavaScript console you should see a dialog box that says 'Hello' appear from your browser. Congrats! That's your first command.
Now if you were able to type
alert('hello')and see a dialog box, let's try entering something more useful.var price = 24And now hit Enter. This sets a variable named
priceto the number24. Let's pretend you paid $24 for a meal and you were trying to figure out how to add a 15% tip, and then split the total (with tip) between you and a friend.Let's see if we can work with our
pricevariable to figure out the amount with tip. If we just typepriceand hit Enter, the console should give us24back as a result. If we typeprice * 2we should get48back, but it's not saving that number anywhere. Let's define a new variable for the total cost of the meal plus tip:var total = price * 1.15Now we have another variable set which should equal
27.599999999999998if we entertotalinto the console again. The last thing we want to do is figure out how much it costs per person if we split the bill 2 ways, so let's do one last command:total / 2We should see
13.799999999999999if everything has worked!That whole process above could be written like this:
var price = 24 var total = price * 1.15 total / 2And we can turn it into a function like this:
function tipCalculator(){ var price = 24 var total = price * 1.15 return total / 2 }Now that it's a function, we can make it smarter by giving it arguments for our original bill value
24, also we can give it another argument for the desired amount of tip we want to leave instead of always1.15, and we can also inform the function between how many people we want to split the total. A more advanced version of this same script might look like this:function tipCalculator(bill,tip,people){ var price = bill var total = price * ((tip + 100) / 100) return total / people }And we could get the same
13.799999999999999result from that function as our original script by running it with the following arguments:I hope this helps break down what a script is, how to get started, and how individual commands and variables can be used together to help make functions that you can use to figure things out :D
JavaScript is very exciting, and the best part is you can write it right in your web browser!