- Learning Linux Shell Scripting
- Ganesh Naik
- 445字
- 2021-06-25 22:02:44
Our first script – Hello World
Since we have learned basic commands in the Linux OS, we will now write our first shell script called hello.sh. You can use any editor of your choice, such as vi, gedit, nano, emacs, geany, and other similar editors. I prefer to use the vi editor:
- Create a new hello.sh file as follows:
#!/bin/bash # This is comment line echo "Hello World" ls date
- Save the newly created file.
The #!/bin/bash line is called the shebang line. The combination of the characters # and ! is called the magic sequence. The shell uses this to call the intended shell, such as /bin/bash in this case. This should always be the first line in a shell script.
The next few lines in the shell script are self-explanatory:
- Any line starting with # will be treated as a comment line. An exception to this would be the first line with #!/bin/bash
- The echo command will print Hello World on the screen
- The ls command will display directory content in the console
- The date command will show the current date and time
We can execute the newly created file with the following commands:
- Technique one:
$ bash hello.sh
- Technique two:
$ chmod +x hello.sh
By running any of the preceding commands, we are adding executable permissions
to our newly created file. You will learn more about file permissions later in this chapter:
$ ./hello.sh
By running the preceding command, we are executing hello.sh as the executable file. With technique one, we passed a filename as an argument to the bash shell.
The output of executing hello.sh will be as follows:
Hello World hello.sh Sun Jan 18 22:53:06 IST 2015
Since we have successfully executed our first script, we will proceed to develop a more advanced script, hello1.sh. Please create the new hello.sh script as follows:
#!/bin/bash # This is the first Bash shell # Scriptname : Hello1.sh # Written by: Ganesh Naik echo "Hello $LOGNAME, Have a nice day !" echo "You are working in directory `pwd`." echo "You are working on a machine called `uname -o`." echo "List of files in your directory is :" ls # List files in the present working directory echo "Bye for now $LOGNAME. The time is `date +%T`!"
The output of executing hello.sh will be as follows:
Hello student, Have a nice day !. Your are working in directory /home/student/work. You are working on a machine called GNU/Linux. List of files in your directory is : hello1.sh hello.sh Bye for now student. The time is 22:59:03!
You will learn about the LOGNAME, uname, and other similar commands as we go through the book.
- 大數(shù)據(jù)管理系統(tǒng)
- 協(xié)作機(jī)器人技術(shù)及應(yīng)用
- 工業(yè)機(jī)器人入門(mén)實(shí)用教程(KUKA機(jī)器人)
- 永磁同步電動(dòng)機(jī)變頻調(diào)速系統(tǒng)及其控制(第2版)
- AWS Administration Cookbook
- Windows 7寶典
- CompTIA Linux+ Certification Guide
- 大數(shù)據(jù)驅(qū)動(dòng)的設(shè)備健康預(yù)測(cè)及維護(hù)決策優(yōu)化
- 大學(xué)C/C++語(yǔ)言程序設(shè)計(jì)基礎(chǔ)
- Azure PowerShell Quick Start Guide
- 人工智能技術(shù)入門(mén)
- 所羅門(mén)的密碼
- AMK伺服控制系統(tǒng)原理及應(yīng)用
- Microsoft Dynamics CRM 2013 Marketing Automation
- 無(wú)人駕駛感知智能