![]() If Statements in Bash Scriptsīash if statements are very useful and allow us to make decisions in bash scripts. You will be asked to provide your name and print the message with other string as shown below: What is Your Name? ![]() Save and close the file, then run the script using the following command: bash input.sh These commands will display a question, take input from the user, and print the value entered while also combining it with a string. To start, create a new bash script named input.sh: nano input.shĪdd the following lines. This will read whatever is entered by the keyboard and save it to the variable. You can accept input from the keyboard and assign a value to a user-defined shell variable using the read command. This is a very simple command, however, it can be useful when debugging your script or when you want to display some information to the user. ![]() This will print the following output: Hello World ![]() Once you have done the above, run the bash script using the following command: bash test.sh Then press Ctrl+X to exit the nano text editor. To save, press Ctrl+O and then press enter to confirm the filename. Continue by adding the following lines: #!/bin/bash ![]() Let’s write our first bash script example by creating a simple shell script file named test.sh that prints the line “Hello World”: nano test.sh You can run a bash script from the terminal, or by executing it from a bash file that is saved on your computer or server. All common distributions of Linux (Debian, Ubuntu, CentOS, Fedora, etc) are all able to run shell scripts. A Linux VPS with root access enabled or a user with sudo privileges. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |