Последнюю версию документа, в виде .bz2 архива, содержащем исходные тексты в формате SGML и HTML, вы найдете на домашней страничке автора. Там же вы найдете и change log.
Посвящения
Посвящается Аните — источнику очарования
Содержание Часть 1. Введение 1. Зачем необходимо знание языка Shell? 2. Для начала о Sha-Bang 2.1. Запуск сценария 2.2. Упражнения Часть 2. Основы 3. Служебные символы 4. Переменные и параметры. Введение. 4.1. Подстановка переменных 4.2. Присваивание значений переменным 4.3. Переменные Bash не имеют типа 4.4.
Специальные типы переменных 5. Кавычки 6. Завершение и код завершения 7. Проверка условий 7.1. Конструкции проверки условий 7.2. Операции проверки файлов 7.3. Операции сравнения 7.4. Вложенные условные операторы if/then 7.5. Проверка степени усвоения материала 8. Операции и смежные темы 8.1. Операторы 8.2.
Числовые константы Часть 3. Углубленный материал 9. К вопросу о переменных 9.1. Внутренние переменные 9.2. Работа со строками 9.3. Подстановка параметров 9.4. Объявление переменных: declare и typeset 9.5. Косвенные ссылки на переменные 9.6. $RANDOM: генерация псевдослучайных целых чисел 9.7.
Создание скриптов bash урок #1
Двойные круглые скобки 10. Циклы и ветвления 10.1. Циклы 10.2. Вложенные циклы 10.3. Управление ходом выполнения цикла 10.4. Операторы выбора 11. Внутренние команды 11.1. Команды управления заданиями 12.
Внешние команды, программы и утилиты 12.1. Базовые команды 12.2. Более сложные команды 12.3. Команды для работы с датой и временем 12.4. Команды обработки текста 12.5. Команды для работы с файлами и архивами 12.6. Команды для работы с сетью 12.7. Команды управления терминалом 12.8. Команды выполнения математических операций 12.9. Прочие команды 13.
Команды системного администрирования 14. Подстановка команд 15. Арифметические подстановки 16. Перенаправление ввода/вывода 16.1. С помощью команды exec 16.2. Перенаправление для блоков кода 16.3.
Область применения 17. Встроенные документы Часть 4. Материал повышенной сложности 18. Регулярные выражения 18.1. Краткое введение в регулярные выражения 18.2. Globbing — Подстановка имен файлов 19. Подоболочки, или Subshells 20.
Ограниченный режим командной оболочки 21. Подстановка процессов 22. Функции 22.1. Сложные функции и сложности с функциями 22.2. Локальные переменные 23. Псевдонимы 24.
Списки команд 25. Массивы 26. Файлы 27. /dev и /proc 27.1. /dev 27.2. /proc 28. /dev/zero и /dev/null 29. Отладка сценариев 30. Необязательные параметры (ключи) 31. Широко распространенные ошибки 32.
Стиль программирования 32.1. Неофициальные рекомендации по оформлению сценариев 33. Разное 33.1. Интерактивный и неинтерактивный режим работы 33.2. Сценарии-обертки 33.3. Операции сравнения: Альтернативные решения 33.4. Рекурсия 33.5. «Цветные» сценарии 33.6. Оптимизация 33.7. Разные советы 33.8. Проблемы безопасности 33.9.
Проблемы переносимости 33.10. Сценарии командной оболочки под Windows 34. Bash, версия 2 35. Замечания и дополнения 35.1. От автора 35.2.
Linux для Начинающих — Скрипты Linux Bash, Часть-1
Об авторе 35.3. Инструменты, использовавшиеся при создании книги 35.3.1. Аппаратура 35.3.2. Программное обеспечение 35.4. Благодарности Литература A. Дополнительные примеры сценариев B. Маленький учебник по Sed и Awk B.1. Sed B.2.
Awk C. Коды завершения, имеющие предопределенный смысл D. Подробное введение в операции ввода-вывода и перенаправление ввода-вывода E. Локализация F. История команд G. Пример файла .bashrc H. Преобразование пакетных (*.bat) файлов DOS в сценарии командной оболочки I. Упражнения I.1. Анализ сценариев I.2. Создание сценариев J. Авторские права
Перечень таблиц 11-1. Идентификация заданий 30-1. Ключи Bash 33-1. Числовые значения цвета в escape-последовательностях B-1. Основные операции sed B-2.
Примеры операций в sed C-1. «Зарезервированные» коды завершения H-1. Ключевые слова/переменные/операторы пакетных файлов DOS и их аналоги командной оболочки H-2. Команды DOS и их эквиваленты в UNIX
Источник: www.opennet.ru
Beginners / BashScripting
Bash scripting is one of the easiest types of scripting to learn, and is best compared to Windows Batch scripting. Bash is very flexible, and has many advanced features that you won’t see in batch scripts.
However if you are a ‘non-computer-savvy’ person that won’t mean a thing to you. Bash is the language that you will learn to love as much of everyday Ubuntu life is done/can be done using the Terminal. You will soon learn that most things can be done through both GUI (Graphical User Interface) and CLI (Command Line Interface), however some things are more easily achieved from one or the other. For example, changing file permissions of a folder and all its sub folders is more easily achieved using cli instead gui.
NOTE: Text that is inside the box are to be entered into a terminal as follows:
If it’s inside a box like this. enter it into a terminal unless instructed to do otherwise. Except this box. Of course. Silly.
You can also just copy and paste if needed.
Intro
In this document we will discuss useful everyday commands, as well as going a bit more in depth into scripting and semi-advanced features of Bash. Bash is not only used to run programs and applications, but it can also be used to write programs or scripts.
Bash — Everyday Ubuntu life
- Creating folders
- Deleting files
- Deleting folders and their sub-folders
- Opening applications as root
- Backing up your files
- Backing up your folders
- Checking system performance
- Check Devices
- Checking wireless connection
Along with many other things, the list above will be the commands we will discuss.
Commands
Creating folders
Creating folders can be done simply in the file manager nautilus by right clicking and selecting ‘Create Folder’, but if you want to do this from a cli environment you would type the following in the terminal:
mkdir /home/joe/Desktop/new_folder
the mkdir (make directory) command creates the folder then the file path tells it where to create the folder.
Deleting files
Deleting files are done with the rm command as follows:
rm /home/joe/file_to_be_deleted
the rm (remove) command is used to remove anything through a cli environment.
Deleting folders and their sub-folders
The command you are about to read can potentially (if used incorrectly) destroy your system!
rm -r /home/joe/useless_Parent_folder
To force (note most of the time you will not need to use -f)
rm -rf /home/joe/useless_Parent_folder
This command is slightly different to the one before, it uses two options ‘-r’ which means recursive (will delete the folder and all sub-folders) and ‘-f’ means force (will not ask for your permission). This command is perfectly fine for deleting a dir and all its sub-dirs. The next commands should !!**!!NEVER!!**!! be run.
Unless you want to say goodbye to your system.
rm -rf /* rm -rf /
This will delete everything from your root folder downwards, which if you did a standard install would be everything.
Running commands as root
When working on the command line, you usually want to work with the default permissions. This way you insure that you won’t accidentally break anything belonging to the system or other users, so long as the system and other users haven’t altered their file permissions. Yet there will be times when you wish to copy a file to a system folder (like /usr/local/bin) to make it available to all users of the system. Only the system administrator (i.e. the user ‘root’) should have permission to alter the contents of system directories like /usr/local/bin. Therefore trying to copy a file (like a program downloaded from the Internet) into that folder is forbidden by default.
cp Downloads/some_downloaded_program /usr/local/bin cp: cannot create regular file `/usr/local/bin/some_downloaded_program’: Permission denied
Since it would be very tedious to always login as root to do administrative work (in fact you should avoid logging in as root with a graphical desktop) you can use the sudo program to execute a single command as root.
sudo cp Downloads/some_downloaded_program /usr/local/bin
The default Ubuntu installation is configured so that the user who was created during system installation is allowed to use this command. It will prompt for the password and execute the command as the root user.
Opening GUI applications as root
Sometimes you will want to edit a config file in your root folder, in order to save changes to this file you need root privileges so we need to open our text editor as root. All you will need is just sudo or pkexec to show a gui password popup.
sudo gedit
Note a lot of kde apps will not work like kate or dolphin.
Backing up your files
To create a backup of a file, we’re going to use the cp (copy) command. The basic syntax for cp is as follows:
cp source_file dest_file
This will copy the ‘source_file’ to the ‘dest_file’. Now, using the previous example, we want to backup ‘/path/to/conf_file.txt’. To accomplish this, we type the following:
sudo cp /path/to/conf_file.txt /path/to/conf_file.txt.old
That’s fine and dandy, but what if I want to copy my file to another directory? Well that’s just as easy. Let’s say instead of copying /path/to/conf_file.txt to the /path/to/ directory, you want to copy it to a directory where you store all of your backup files, say /my/backup/folder/. In order to accomplish this you would type:
cp /path/to/conf_file.txt /my/backup/folder/ #saves conf_file.txt to /my/backup/folder/ #OR cp /path/to/conf_file.txt /my/backup/folder/conf_file_new_name.txt
***This is a typical safety measure that has saved many users in the past from a complete disaster.***
Okay, so we know how to copy a file: a) to a different filename and b) to a different folder. But how do we copy entire directories?
Backing up your Directories
To backup one directory to another, we introduce cp -r (recursive) option. The basic syntax is as follow:
cp -r /directory/to/be/copied/ /where/to/copy/to/
So if we wanted to copy all of the contents of our /path/to/ folder to our /my/backup/folder, we would type the following:
cp -r /path/to/ /my/backup/folder/foldername #foldername can be whatever you want the foldername to be
Checking system performance
If your computer starts to lag, you can see which applications are using the most CPU power with this command:
This is generally the same information given as the GUI application ‘System Monitor’.
Check Devices
If a USB device stops working, you may want to see if it is still connected/detected. To check if a device is connected/detected, type the following:
lsusb
As PCI devices are checked with:
lspci
Show network Information
To see the status of your network connection, use the command:
ip addr
This command will show you your ip, what type of connection you are using, etc.
Show wireless information
Like the command ip stated above, you can use iwconfig to check the settings of your wireless connection without editing anything. In a terminal enter:
iwconfig
This also shows packets sent/received.
Scripting
NOTE: The commands given in the scripting section are to be put into the text editor and not in the terminal unless instructed otherwise.
Bash is primarily a scripting language, so it would be a crime not to talk about scripting. Let’s dive straight in with a bash script. More precisely the infamous «Hello World» script. You can create a bash script by opening your favorite text editor to edit your script and then saving it (typically the .sh file extension is used for your reference, but is not necessary. In our examples, we will be using the .sh extension but instead Linux uses #!/path/to/runtime or in this case #!/bin/bash).
#!/bin/bash echo «Hello, World»
The first line of the script just defines which interpreter to use. NOTE: There is no leading whitespace before #!/bin/bash. That’s it, simple as that. To run a bash script you first have to have the correct file permissions. We do this with chmod command in terminal (change mode) as follows:
chmod a+x /where/i/saved/it/hello_world.sh #Gives everyone execute permissions # OR chmod 700 /where/i/saved/it/hello_world.sh #Gives read,write,execute permissions to the Owner
This will give the file the appropriate permissions so that it can be executed. Now open a terminal and run the script like this:
/where/i/saved/it/hello_world.sh
Hopefully you should have seen it print Hello, World onto your screen. If so well done! That is your first Bash script.
TIP If you type:
You will see the directory that you are currently working in (pwd stands for ‘print working directory’). If your current working directory is /where/i/saved/it/, then you can shorten the above command to:
prompt$ pwd /where/i/saved/it prompt$ ./hello_world.sh
Now, lets get to more interesting aspects of Bash programming, Variables!
Variables
Variables basically store information. You set variables like this using text editor:
var=»FOO»
‘var’ can be anything you want as long as it doesn’t begin with a number. «FOO» can be anything you want.
To access the information from the variable you need to put a ‘$’ in front of it like this:
var=»FOO» echo $var
Try entering those lines into a terminal one at a time; you will see that the first one just gives you another prompt and the second one prints FOO.
But that’s all a bit boring. So let’s make a script to ask the user for some information and then echo that information.
#!/bin/bash clear echo «Please enter your name» read name echo «Please enter your age» read age echo «Please enter your sex. Male/Female» read sex echo «So you’re a $age year old $sex called $name»
read allows the user to input information where it is then stored in the variable defined after the read. read variable would take whatever input the user entered and store it in $variable. We then access this with echo and set up a neat sentence.
This script is reasonably messy though; read has another function that could halve the size of this script.
clear read -p «Please enter your name : » name read -p «Please enter your age : » age read -p «Please enter your sex. Male/Female : » sex echo «So you’re a $age year old $sex called $name»
That is more efficient code. However it’s still a bit messy when run. A solution?
Good old white spaces!
clear read -p «Please enter your name : » name echo «» read -p «Please enter your age : » age echo «» read -p «Please enter your sex. Male/Female : » sex echo «» echo «So you’re a $age year old $sex called $name»
Now we have an efficient and clean Bash script.
If Statements
An if statement can be used to check for something and do something else depending on the outcome of the check. For example, if I had an ‘apple’, I would want to make sure it’s still an ‘apple’ and not an ‘orange’ because I don’t like Oranges!
The syntax for an if statement is
if [something] then elif then elif then . etc. else fi
The else if statement or (elif) is not necessary, but it can be used if needed.
An if statement to check if our $fruit variable is an ‘apple’ would look like this
echo «Please enter type of fruit» read fruit if [ $fruit = apple ] then echo «Good, I like Apples» else echo «Oh no, I hate Oranges!» fi
Just to explain this statement,
if [ the contents of $fruit is ‘apple’ ] then say «Good, I like Apples» if it’s not, then say «Oh no, I hate Oranges!» finish
If statements are an easy concept to grasp as they are similar to the «if» used in spoken English. But say you wanted to have 4 or 5 checks, the answer may be to write 4 or 5 statements but that’s not the most practical way. This is where elif comes in handy.
if [ $fruit = apple ] then echo «Good, I like Apples» elif [ $fruit = pear ] then echo «Good, I like Pears» elif [ $fruit = banana ] then echo «Good, I like Bananas» else echo «Oh no, I hate Oranges!» fi
This saves us from from repetitive scripting. There are better ways to check what the fruit is, but we won’t go into that now.
Storing application stdout to a variable:
Application stdout ( what you see on the terminal screen, with an un-piped application ) can be saved and used in Bash. The simplest and most elegant way is to use command substitution, by wrapping the code in $(. )
Example
fooVar=$(who) echo $fooVar
This code should output the current users, their respective ttys, and date of login. Note that this strips newlines. Be sure to do any parsing in line ( | grep, etc ) and then pass it to a variable. We will try this again, but grep for tty7, the GUI’s tty.
Example 2
fooVar=$(who | grep tty7) echo $fooVar
This should output the single user that is currently logged into the WM. Let’s move on to more advanced data manipulation within command substitution.
FUNctions
Bash lets you create a function on the fly, really handy if you plan on using a code block more then once. Functions reduce the amounts of editing you have to do in a script, if and when you have to update your script. Let’s get to it!
Example
Here is an example script:
echo «echo is Called» echo «Functions are FUN!» echo «echo is Called»
Although this example is simple, you can see that if you want to change «echo is Called» to say «foo is Called» you would have to edit twice.
Below is the same app using functions.
echoFunction() echo «echo is Called» > fooBar() echo «Functions are FUN!» > echoFunction; fooBar; echoFunction; # You call functions without (), just the function name then a semicolon.
This example, as you can see may be longer now, but you can imagine how, adding features, this will eliminate code and reduce complexity. Also, you can see if you want to change the echo call, you have to edit one line, not two.
Debugging
I always find it useful to trace a script to find out why something does not work as expected. To trace, start it through bash explicitly and use the -x option, like so:
bash -x ./script.sh
This will write each command to standard error (preceded by a ‘+ ’) before it is executed.
Other Scripting Languages related to Bash
tr
tr is one of the most basic applications to pipe data through that uses a basic scripting syntax. In this case, it accepts Regular Expressions. Let’s do a normally complicated task, transforming a string to all uppercase.
Example
read foo var=$(echo $foo | tr «» «») # Matches a through z # matches A through Z echo $var
The output should look something like this:
this is a test THIS IS A TEST
tr also can TRanslate strings, so let’s translate all «tar» in $foo to «bar».
Example
echo «Type in: I love tars» read foo var=$(echo $foo | tr «t» «b») echo $var
the output should look something like this:
I love tars I love bars
AWK
AWK ( Short for Aho, Weinberger print $2 >’
Single quotes are used to pass the awk command(s). The curly braces are to use the awk language (for stuff like prints, ifs, etc.). Print prints the column passed given by the $ markup, space delimited.
The awk -v option allow passing a shell value into an awk variable, the $8 is a field variable (column 8 of the ps -ef command’s output) and the operator ~ is a regular expression match.
There are a lot more commands than the print command, including if statements, etc., and is worth looking into if you are interested in what you see here!
SED
sed is one of the most complicated scripting languages on the GNU / Linux system. I am only going to cover the s/ command here.
Basic Substitution
Try this out to show that sed can not only replace inline, but unlike tr, replace with a longer or shorter string than before.
read foo echo $foo | sed «s/foo/bars/»
When this command is run, it should substitute the first appearance of «foo» with «bars».
This is an example of the output.
I love to go to foo I love to go to bars
Beginners/BashScripting (последним исправлял пользователь lnee 2022-02-01 17:36:41)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
Источник: help.ubuntu.com
Самоучитель по написанию скриптов Bash для начинающих. Часть 1
Выпускаем серию статей о том, как писать скрипты Bash. Подойдет начинающим!
Что такое Bash/Shell/Scripting
Bash — это интерпретатор командного языка. Он широко доступен в различных операционных системах и является командным интерпретатором по умолчанию в большинстве систем GNU/Linux. Название является акронимом для ‘Bourne-Again SHell’.
Shell — это макропроцессор, который позволяет выполнять команды в интерактивном или неинтерактивном режиме.
Скриптинг позволяет автоматически выполнять команды, которые в противном случае выполнялись бы интерактивно одна за другой.
Основы сценариев оболочки Bash
Не отчаивайтесь, если вы не поняли ни одного из приведенных выше определений. Это совершенно нормально, ведь именно поэтому вы читаете эту статью.
Если вы не знали, Bash Scripting является обязательным навыком для любой работы в Linux системного администрирования, даже если работодатель может не требовать этого в явном виде.
Что такое Shell
Скорее всего, в данный момент вы сидите за компьютером, открыли окно терминала и задаетесь вопросом: «Что же мне делать с этой штукой?».
Так вот, в окне терминала перед вами находится shell, а shell позволяет вам с помощью команд взаимодействовать с компьютером, следовательно, получать или хранить данные, обрабатывать информацию и выполнять различные другие простые или даже очень сложные задачи.
Попробуйте это прямо сейчас! Используйте клавиатуру и введите несколько команд, таких как date, cal, pwd или ls, после чего нажмите клавишу ENTER.
Вы только что сделали то, что с помощью команд и командной оболочки взаимодействовали с компьютером, чтобы получить текущую дату и время (date), просмотреть календарь (cal), проверить расположение текущего рабочего каталога (pwd) и получить список всех файлов и каталогов, расположенных в нем (ls).
*у нас в папке пока ничего нет, поэтому команда ls не дала вывода.
Что такое скриптинг (сценарии Bash)
Теперь представьте, что выполнение всех вышеперечисленных команд является вашей ежедневной задачей. Каждый день вы должны безошибочно выполнять все вышеперечисленные команды, а также сохранять наблюдаемую информацию. Вскоре это станет чрезвычайно утомительной задачей, обреченной на провал. Поэтому очевидная идея — придумать способ выполнения всех команд вместе. Именно здесь скриптинг становится вашим спасением.
Чтобы понять, что подразумевается под сценариями, используйте shell в сочетании с вашим любимым текстовым редактором, например vi, чтобы создать новый файл task.sh, содержащий все вышеперечисленные команды, каждую в отдельной строке. После этого сделайте новый файл исполняемым с помощью команды chmod с опцией +x. И наконец, запустите новый скрипт, добавив к его имени ./.
Как видите, с помощью сценариев можно автоматизировать любое взаимодействие с оболочкой. Более того, теперь можно автоматически выполнять наш новый сценарий task.sh ежедневно в любое заданное время с помощью планировщика заданий cron и сохранять вывод сценария в файл при каждом его выполнении. Однако это уже другая история, а пока давайте сосредоточимся на предстоящей задаче.
Что такое Bash
До сих пор мы рассматривали shell и сценарии. А что насчет Bash? Где находится bash? Как уже упоминалось, bash является интерпретатором по умолчанию во многих системах GNU/Linux, поэтому мы использовали его, даже не осознавая этого. Вот почему наш предыдущий сценарий оболочки работает даже без определения bash в качестве интерпретатора. Чтобы узнать, какой у вас интерпретатор по умолчанию, выполните команду echo $SHELL:
Существуют различные другие интерпретаторы оболочки, такие как Korn shell, C shell и другие. Поэтому хорошей практикой является явное определение интерпретатора оболочки, который будет использоваться для интерпретации содержимого скрипта.
Чтобы определить интерпретатор вашего скрипта как Bash, сначала найдите полный путь к его исполняемому двоичному файлу с помощью команды which, добавьте к нему префикс shebang #! и вставьте его в качестве первой строки вашего скрипта. Существуют и другие способы определения интерпретатора командной оболочки, но это — надежное начало.
С этого момента все наши сценарии будут включать определение интерпретатора оболочки #!/bin/bash.
Имена файлов и разрешения
Далее давайте кратко обсудим права доступа к файлам и имена файлов. Вы, наверное, уже заметили, что для выполнения сценария shell файл должен быть сделан исполняемым с помощью команды chmod +x FILENAME. По умолчанию все вновь созданные файлы не являются исполняемыми, независимо от суффикса расширения файла.
На самом деле, расширение файла в системах GNU/Linux не имеет никакого значения, кроме того, что при выполнении команды ls для перечисления всех файлов и каталогов сразу становится ясно, что файл с расширением .sh — это, скорее всего, сценарий оболочки, а файл с .jpg — сжатое с потерями изображение.
В системах GNU/Linux команда file может быть использована для определения типа файла. Как видно из приведенного ниже примера, расширение файла не имеет никакого значения, а интерпретатор оболочки, в данном случае, имеет больший вес.
Таким образом, имя shell-сценария 0_xyz вполне допустимо, но по возможности его следует избегать.
Выполнение скриптов Bash
Далее поговорим об альтернативном способе выполнения сценариев bash. В упрощенном виде сценарий bash — это не что иное, как текстовый файл, содержащий инструкции, которые должны быть выполнены в порядке сверху вниз. То, как интерпретируются инструкции, зависит от определенного shebang или способа выполнения скрипта. Рассмотрим следующий видеопример:
Другим способом выполнения сценариев bash является явный вызов интерпретатора bash, например, $ bash date.sh, что позволяет выполнить сценарий без необходимости делать сценарий оболочки исполняемым и без объявления shebang непосредственно в сценарии оболочки. При явном вызове исполняемого двоичного файла bash содержимое нашего файла date.sh загружается и интерпретируется как сценарий оболочки Bash.
Относительный и абсолютный путь
Наконец, прежде чем мы запрограммируем наш первый официальный сценарий оболочки bash, давайте кратко обсудим навигацию оболочки и разницу между относительным и абсолютным путем к файлу.
Наверное, лучшая аналогия для объяснения относительного и абсолютного пути к файлу — это представить файловую систему GNU/Linux как многоэтажное здание. Корневой каталог (входная дверь здания), обозначенный /, обеспечивает вход во всю файловую систему (здание), а значит, дает доступ ко всем каталогам (уровням/комнатам) и файлам (людям).
Чтобы перейти в комнату 1 на уровне 3, нам сначала нужно войти в главную дверь /, затем пройти на уровень 3 level3/ и оттуда войти в комнату 1. Следовательно, абсолютный путь к этой конкретной комнате в здании — /level3/room1. Отсюда, если мы хотим посетить комнату2 на уровне 3, нам сначала нужно покинуть наше текущее местоположение — комнату1, введя ../, а затем включить название комнаты room2. Мы взяли относительный путь к room2, который в данном случае будет ../room2. Мы уже находились на уровне 3, поэтому не было необходимости покидать все здание и идти по абсолютному пути через главный вход /level3/room2.
К счастью, в GNU/Linux есть простой инструмент компас, который поможет вам ориентироваться в файловой системе в виде команды pwd. Эта команда при выполнении всегда выводит ваше текущее местоположение. В следующем примере используются команды cd и pwd для навигации по файловой системе GNU/Linux с использованием абсолютных и относительных путей.
Быстрый совет:
Выполните команду cd без аргументов, чтобы мгновенно перейти в домашний каталог пользователя из любого места. Выполните команду cd — для переключения между двумя последними посещенными местами. В каком каталоге вы оказались после выполнения команд cd ~ и cd .
Навигация по файловой системе GNU/Linux — это простая, но для многих очень запутанная тема. Ознакомьтесь с навигацией по файловой системе GNU/Linux, прежде чем переходить к следующим разделам этого учебника.
Hello World Bash Shell Скрипт
Теперь пришло время написать наш первый, самый простой сценарий оболочки bash. Целью этого сценария является не что иное, как вывод «Hello World» с помощью команды echo на вывод терминала. Используя любой текстовый редактор, создайте новый файл с именем hello-world.sh, содержащий следующий код:
#!/bin/bash
echo «Hello World»
После этого сделайте ваш скрипт исполняемым с помощью командыchmod и запустите его, используя относительный путь ./hello-world.sh:
$ chmod +x hello-world.sh
$ linuxconfig.org:~$ ./hello-world.sh
Hello World
$
Следующий видеопример предлагает альтернативный способ создания приведенного выше сценария hello-world.sh. В нем используется команда which для вывода полного пути к интерпретатору bash. Этот вывод одновременно перенаправляется с помощью знака перенаправления > и одновременно создается новый файл hello-world.sh.
Источник: dzen.ru