Linux Bible. Christopher Negus

Чтение книги онлайн.

Читать онлайн книгу Linux Bible - Christopher Negus страница 72

Linux Bible - Christopher Negus

Скачать книгу

anything? echo "Sorry, that name was not found in the phone list" exit 1 else grep -i "$*" $PHONELIST fi fi exit 0

      So, if you created the telephone list file as ph in your current directory, you could type the following from the shell to try out your ph script:

       $ chmod 755 ph $ ./ph new "Mary Jones" 608-555-1212 Mary Jones 608-555-1212 added to database $ ./ph Mary Mary Jones 608-555-1212

      The chmod command makes the ph script executable. The ./ph command runs the ph command from the current directory with the new option. This adds Mary Jones as the name and 608-555-1212 as the phone number to the database ($HOME/.phonelist.txt). The next ph command searches the database for the name Mary and displays the phone entry for Mary. If the script works, add it to a directory in your path (such as $HOME/bin).

      Backup script

      Because nothing works forever and mistakes happen, backups are just a fact of life when dealing with computer data. This simple script backs up all of the data in the home directories of all of the users on your Fedora or RHEL system.

       #!/bin/bash # (@)/my_backup # A very simple backup script # # Change the TAPE device to match your system. # Check /var/log/messages to determine your tape device. TAPE=/dev/rft0 # Rewind the tape device $TAPE mt $TAPE rew # Get a list of home directories HOMES=`grep /home /etc/passwd | cut -f6 -d':'` # Back up the data in those directories tar cvf $TAPE $HOMES # Rewind and eject the tape. mt $TAPE rewoffl

      Writing shell scripts gives you the opportunity to automate many of your most common system administration tasks. This chapter covered common commands and functions that you can use in scripting with the bash shell. It also provided some concrete examples of scripts for doing backups and other procedures.

      In the next chapter, you transition from learning about user features into examining system administration topics. Chapter 8, “Learning System Administration,” covers how to become the root user, as well as how to use administrative commands, monitor log files, and work with configuration files.

      Use these exercises to test your knowledge of writing simple shell scripts. These tasks assume you are running a Fedora or Red Hat Enterprise Linux system (although some tasks work on other Linux systems as well). If you are stuck, solutions to the tasks are shown in Appendix B (although in Linux, there are often multiple ways to complete a task).

      1 Create a script in your $HOME/bin directory called myownscript. When the script runs, it should output information that appears as follows: Today is Sat Jan 4 15:45:04 EST 2020. You are in /home/joe and your host is abc.example.com.Of course, you need to read in your current date/time, current working directory, and hostname. Also, include comments about what the script does and indicate that the script should run with the /bin/bash shell.

      2 Create a script that reads in three positional parameters from the command line, assigns those parameters to variables named ONE, TWO, and THREE, respectively, and outputs that information in the following format: There are X parameters that include Y. The first is A, the second is B, the third is C.Replace X with the number of parameters and Y with all parameters entered. Then replace A with the contents of variable ONE, B with variable TWO, and C with variable THREE.

      3 Create a script that prompts users for the name of the street and town where they grew up. Assign town and street to variables called mytown and mystreet, and output them with a sentence that reads as shown below (of course, $mystreet and $mytown will appear with the actual town and street the user enters): The street I grew up on was $mystreet and the town was $mytown

      4 Create a script called myos that asks the user, “What is your favorite operating system?” Output an insulting sentence if the user types “Windows” or “Mac.” Respond “Great choice!” if the user types “Linux.” For anything else, say “Is <what is typed in> an operating system?”

      5 Create a script that runs through the words moose, cow, goose, and sow through a for loop. Have each of those words appended to the end of the line “I have a… .”

      Конец ознакомительного фрагмента.

      Текст предоставлен ООО «ЛитРес».

      Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.

      Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.

/9j/4AAQSkZJRgABAQEBLAEsAAD/7SB6UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAA8cAVoAAxsl RxwCAAACAAAAOEJJTQQlAAAAAAAQzc/6fajHvgkFcHaurwXDTjhCSU0EOgAAAAAA5QAAABAAAAAB AAAAAAALcHJpbnRPdXRwdXQAAAAFAAAAAFBzdFNib29sAQAAAABJbnRlZW51bQAAAABJbnRlAAAA AEltZyAAAAAPcHJpbnRTaXh0ZWVuQml0Ym9vbAAAAAALcHJpbnRlck5hbWVURVhUAAAAAQAAAAAA D3ByaW50UHJvb2ZTZXR1cE9iamMAAAAMAFAAcgBvAG8AZgAgAFMAZQB0AHUAcAAAAAAACnByb29m U2V0dXAAAAABAAAAAEJsdG5lbnVtAAAADGJ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AAAABgAAAAAAAThCSU0D+AAAAAAAcAAA//////// /////////////////////wPoAAAAAP////////////////////////////8D6AAAAAD///////// ////////////////////A+gAAAAA/////////////////////////////wPoAAA4QklNBAgAAAAA ABAAAAABAAACQAAAAkAAAAAAOEJJTQQeAAAAAAAEAAAAADhCSU0EGgAAAAADTwAAAAYAAAAAAAAA AAAACtcAAAilAAAADQA5ADcAOAAxADEAMQA5ADUANwA4ADgAOAA4AAAAAQAAAAAAAAAAAAAAAAAA AAAAAAABAAAAAAAAAAAAAAilAAAK1wAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAA ABAAAAABAAAAAAAAbnVsbAAAAAIAAAAGYm91bmRzT2JqYwAAAAEAAAAAAABSY3QxAAAABAAAAABU b3AgbG9uZwAAAAAAAAAATGVmdGxvbmcAAAAAAAAAAEJ0b21sb25nAAAK1wAAAABSZ2h0bG9uZwAA CKUAAAAGc2xpY2VzVmxMcwAAAAFPYmpjAAAAAQAAAAAABXNsaWNlAAAAEgAAAAdzbGljZUlEbG9u ZwAAAAAAAAAHZ3JvdXBJRGxvbmcAAAAAAAAABm9yaWdpbmVudW0AAAAMRVNsaWNlT3JpZ2luAAAA DWF1dG9HZW5lcmF0ZWQAAAAAVHlwZWVudW0AAAAKRVNsaWNlVHlwZQAAAABJbWcgAAAABmJvdW5k c09iamMAAAABAAAAAAAAUmN0MQAAAAQAAAAAVG9wIGxvbmcAAAAAAAAAAExlZnRsb25nAAAAAAAA AABCdG9tbG9uZwAACtcAAAAAUmdodGxvbmcAAAilAAAAA3VybFRFWFQAAAABAAAAAAAAbnVsbFRF WF