Questions tagged [scripting]

This tag is non-specific. Use a specific script tag instead. The following tags are much more specific: [awk] [bash] [bat] [google-apps-script] [javascript] [perl] [php] [python] [ruby] [sh] [powershell]

Scripting is a form of programming generally characterized by low formality, loose typing, and no requirement for explicit compilation. There are numerous scripting languages, and these are used in a wide variety of scenarios - command-line applications, GUIs, server-side applications, extension modules.

Scripting started out as storing a sequence of commands as a text file, and then feeding the text file into an interpreter or shell.

Early scripting-languages would just read, process, and execute one command at a time. There was no requirement to compile the script into a separate executable file. The source was the program.

Most modern scripting languages feature "interpreters" that perform an internal compilation step, and execute an intermediate code; but this is done automatically, transparently to the developer and user.

Examples of scripting languages are the Unix shells (Bourne and Korn shell, Bash, [t]csh, and zsh), Perl, and the special purpose language, AWK. Other examples include JCL (job control language), RPG, REXX, Windows/DOS BAT files, and MUMPS (used in medical applications). The world's most popular language, JavaScript, is a scripting language.

Over the last ten years, Powershell has become a popular scripting language under Windows, and has been extended to multiple platforms.

Most scripting languages support features such as:

  • dynamic typing
  • direct invocation of external commands
  • direct interaction with the file system
  • basic string processing
  • error control

Scripting languages are traditionally used for common system administration tasks like backups, and software installation and configuration.

Many scripting languages originated as special-purpose tools, and later evolved into powerful general-purpose languages over time. Examples here are Perl and JavaScript.

Over time, the distinction between scripting languages and more "traditional" compiled languages has fuzzed.

For example, interpreters for Python can actually cache its intermediate code, for faster startup times, much like programs written in traditional compiled languages. Conversely, traditional compiled languages are in some ways becoming more script-like, supporting dynamic data typing. The VAR type in the original Visual Basic is an example here, as is the dynamic variable type supported in C# 4.0.

Related tags

17656 questions
3914
votes
20 answers

How do I tell if a file does not exist in Bash?

This checks if a file exists: #!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "File $FILE exists." else echo "File $FILE does not exist." fi How do I only check if the file does not exist?
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
2839
votes
38 answers

How do I split a string on a delimiter in Bash?

I have this string stored in a variable: IN="bla@some.com;john@home.com" Now I would like to split the strings by ; delimiter so that I have: ADDR1="bla@some.com" ADDR2="john@home.com" I don't necessarily need the ADDR1 and ADDR2 variables. If…
stefanB
  • 77,323
  • 27
  • 116
  • 141
2823
votes
17 answers

How to mkdir only if a directory does not already exist?

I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that…
Spike Williams
  • 35,795
  • 13
  • 48
  • 60
2518
votes
41 answers

How do I parse command line arguments in Bash?

Say, I have a script that gets called with this line: ./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile or this one: ./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile What's the accepted way of parsing this such that in each…
Redwood
  • 66,744
  • 41
  • 126
  • 187
1839
votes
39 answers

How do I prompt for Yes/No/Cancel input in a Linux shell script?

I want to pause input in a shell script, and prompt the user for choices. The standard Yes, No, or Cancel type question. How do I accomplish this in a typical bash prompt?
Myrddin Emrys
  • 42,126
  • 11
  • 38
  • 51
1418
votes
27 answers

How can I declare and use Boolean variables in a shell script?

I tried to declare a Boolean variable in a shell script using the following syntax: variable=$false variable=$true Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using…
hassaanm
  • 14,539
  • 4
  • 21
  • 20
1388
votes
28 answers

How to count lines in a document?

I have lines like these, and I want to know how many lines I actually have... 09:16:39 AM all 2.00 0.00 4.00 0.00 0.00 0.00 0.00 0.00 94.00 09:16:40 AM all 5.00 0.00 0.00 4.00 0.00 0.00 0.00 0.00 …
Alucard
  • 16,628
  • 7
  • 24
  • 23
984
votes
12 answers

How does "cat << EOF" work in bash?

I needed to write a script to enter multi-line input to a program (psql). After a bit of googling, I found the following syntax works: cat << EOF | psql ---params BEGIN; `pg_dump ----something` update table .... statement ...; END; EOF This…
hasen
  • 161,647
  • 65
  • 194
  • 231
961
votes
17 answers

How to run a PowerShell script

How do I run a PowerShell script? I have a script named myscript.ps1 I have all the necessary frameworks installed I set that execution policy thing I have followed the instructions on this MSDN help page and am trying to run it like…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
930
votes
9 answers

In a Bash script, how can I exit the entire script if a certain condition occurs?

I'm writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I'll just abort the tests. Is there a way I can do this without wrapping the entire script inside…
samoz
  • 56,849
  • 55
  • 141
  • 195
809
votes
12 answers

How do I compare two string variables in an 'if' statement in Bash?

I'm trying to get an if statement to work in Bash (using Ubuntu): #!/bin/bash s1="hi" s2="hi" if ["$s1" == "$s2"] then echo match fi I've tried various forms of the if statement, using [["$s1" == "$s2"]], with and without quotes, using =, ==…
Mr Shoubs
  • 14,629
  • 17
  • 68
  • 107
773
votes
13 answers

How can I set the current working directory to the directory of the script in Bash?

I'm writing a Bash script. I need the current working directory to always be the directory that the script is located in. The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not…
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
770
votes
18 answers

Find and Replace Inside a Text File from a Bash Command

What's the simplest way to do a find and replace for a given input string, say abc, and replace with another string, say XYZ in file /tmp/file.txt? I am writting an app and using IronPython to execute commands through SSH — but I don't know Unix…
Ash
  • 24,276
  • 34
  • 107
  • 152
765
votes
20 answers

How can I remove the first line of a text file using bash/sed script?

I need to repeatedly remove the first line from a huge text file using a bash script. Right now I am using sed -i -e "1d" $FILE - but it takes around a minute to do the deletion. Is there a more efficient way to accomplish this?
Brent
  • 16,259
  • 12
  • 42
  • 42
762
votes
26 answers

How do I know the script file name in a Bash script?

How can I determine the name of the Bash script file inside the script itself? Like if my script is in file runme.sh, then how would I make it to display "You are running runme.sh" message without hardcoding that?
Ma99uS
  • 10,605
  • 8
  • 32
  • 42
1
2 3
99 100