1

I am just starting to learn the basics of Linux terminal and I'm a little confused on converting terminal commands into shell script. How would I approach creating a script file in the Linux terminal and creating a shell script version of my code below:

curl -O https://people.sc.fsu.edu/~jburkardt/data/csv/cities.csv
head -n 12 cities.csv
4
  • Is the URL predetermined or you need to pass it to the script? Sep 27, 2020 at 18:45
  • I need to pass it to the script Sep 27, 2020 at 18:55
  • Put #!/bin/sh atop the file, and apply chmod +x file.sh to make it executable.Call it like ~$ ./file.sh.
    – 0x0584
    Sep 27, 2020 at 22:26
  • If you are interested in learning more, consider GreyCat&Lhunath's Bash Guide.
    – AdminBee
    Nov 11, 2020 at 8:30

2 Answers 2

7

Using a text editor to create your script

Scripts are saved to text files. You can create one in a text editor, like gedit or nano:

$ nano yourscript.sh

The filename in general and the filename extension in particular does not matter. In your script file, you have to declare the executable of the interpreter to run it (a shebang). Then add your code. To save your script in nano, press Ctrl+X, then type y and hit Enter.

Composing your script

If you pass arguments to the script, you can reference them in your code by number (the first passed argument is $1, the second one is $2, and so on). $0 is the script's name itself.

As a general rule, always use double quotes around variables.

Taking your code, for example, the script might be:

#! /bin/bash
curl -O "$1"
head -n 12 cities.csv

There can be a space after #!, but it is not necessary. In most cases, you can check $0 variable in terminal for the shell executable interpreting your commands in terminal:

$ echo $0
/bin/bash

Improving your code

Assuming that all you want are the first 12 lines of the downloaded file, you can skip saving the downloaded file to disk and pipe it to the next command:

#! /bin/bash
curl -s "$1" | head -n 12

As a bonus, it allows you to omit the filename in your script.

Running the script

Assuming you save your script as yourscript.sh, add permission to execute it first:

$ chmod +x yourscript.sh

Then, to run it, you need to specify the path to it and provide the arguments. If it is in the current directory, you can specify the path to it by prepending ./ like:

$ ./yourscript.sh 'https://people.sc.fsu.edu/~jburkardt/data/csv/cities.csv'
3
  • Do I put the '#!/bin/bash' code first or the code to make it executable first? Also how do I create the file in the first place bc when I enter 'chmod +x yourscript.sh' or './yourscript.sh', I get the message "cannot access ‘scriptex.sh’: No such file or directory" Sep 27, 2020 at 19:11
  • 3
    Make sure you use quotes for all variables containing user input (security!): curl -O "$1" Sep 29, 2020 at 18:25
  • @user10649856, this is old comment but for future users wondering about this. Regarding the first question "to put #!/bin/bash code first or to make file executable at the first place" -> it is irrelevant which order you will use. Regarding the second question "how to create a file" -> a lot of ways: touch some_file, echo >some_file, >some_file, vi some_file, ...
    – Damir
    Jan 4 at 13:42
3

All the command you type one by one at the prompt, can simply be put in a script file and executed once.

  1. The only requirement is the interpreter line, which should be at the start of file

      #!/bin/sh
    

    you can write script for other like bash (#!/bin/bash) or zsh (#!/bin/zsh)

  2. Make you script executable by running chmod +x scriptFile.sh (sh extension is good to have, as it indicates that it's a script file)

  3. Run the script by

    ./scriptFile.sh

Optional: You can add the directory path to PATH variable, to be able to execute the script from any directory by just calling scriptFile.sh just like you invoke any other shell command ls, or cat

2
  • One more question, which command saves the script file? Sep 27, 2020 at 18:57
  • @user10649856 you need to write the script file via normal text editor whichever you are comfortable with.
    – mtk
    Oct 1, 2020 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.