Senin, 31 Juli 2023

Bash Basics Series #8: For, While and Until Loops

Bash Basics Series #8: For, While and Until Loops

Loops are a powerful feature in any programming language. If you do not know already, the loops are a way to repeat the code based on certain criteria.

For example, imagine that you have to print the numbers from 1 to 10. You can write the echo command ten times but that's very primitive. You use a loop and in 3-4 lines of code, it can be done.

That's the simplest of the examples I could think of. I am going to share actual useful examples while I discuss the bash loops with you.

There are three types of loops in Bash:

  • For
  • While
  • Until

I'll show all three kinds of looping in the tutorial. Let's start with the most common one.

For loop in bash

Here's the syntax for 'for loop' in bash:

for arg in LIST; do
 commands
done

The LIST here could be an array or a list of items. Brace expansions are also popular for looping.

Take the simplest scenario I mentioned in the beginning. Let's print numbers from 1 to 10 using for loop:

#!/bin/bash

for num in {1..10}; do
        echo $num
done

If you run it, you should see an output like this:

abhishek@itsfoss:~/bash_scripts$ ./for-loop.sh
1
2
3
4
5
6
7
8
9
10

You could have also used for num in 1 2 3 4 5 6 7 8 9 10; do but using the brace expansion makes the code look shorter and smarter.

{..} is used for expanding on a pattern. You use {d..h} and it is equivalent to d e f g h . More on brace expansion can be found in this article.

Using Brace Expansion in Bash Shell
Brace expansion in the bash shell is a lesser known but an awesome feature. Learn about using them like a Pro Linux user with practical examples.
Bash Basics Series #8: For, While and Until Loops
💡
If you are familiar with C programming, you may like using the C-styled for loops in bash:

for ((i = 0 ; i < 10 ; i++)); do
echo $i
done

Let's see another example that displays all the contents of an array in bash:

#!/bin/bash

distros=(Ubuntu Fedora Debian Alpine)

for i in "${distros[@]}"; do
        echo $i
done

If you run the script, it will display all the distros defined in the array:

Ubuntu
Fedora
Debian
Alpine

While loop in bash

The while loop tests a condition and then keeps on looping as long as the condition is true.

while [ condition ]; do
  commands
done

If you take the previous example, it can be rewritten using the while loop like this:

#!/bin/bash

num=1
while [ $num -le 10 ]; do
        echo $num
        num=$(($num+1))
done

As you can see, you had to define the variable num to 1 first and then in the loop body, you increase the value of num by 1. The while loop checks the condition and runs it as long as num is less than or equal to 10.

Thus, running the script now will show the exact result you saw earlier with for loop.

1
2
3
4
5
6
7
8
9
10

Let's see another example. Here's a bash script that takes a number as an argument and displays its table.

#!/bin/bash

echo "Table for $1 is:"
index=1
while [ $index -le 10 ]; do
        echo $(($1*$index))
        index=$(($index+1))
done

If you are confused about the use of $1, it represents the first argument passed to the script. Check out chapter 3 of this series for more details.

If you run the script, it should show this output:

abhishek@itsfoss:~/bash_scripts$ ./table.sh 2
Table for 2 is:
2
4
6
8
10
12
14
16
18
20

Until loop in bash

This is the lesser-used loop format. It behaves similarly to the while loop. The difference here is that the loop runs until the condition it checks is false. I'll explain it in a bit. Let's see its syntax first.

until [ condition ]; do
  commands
done

Now, if I have to use the same example of printing numbers from 1 to 10 using until loop, it would look like this:

#!/bin/bash

num=1
until [ $num -gt 10 ]; do
        echo $num
        num=$(($num+1))
done

The difference is in the condition; the rest remains the same.

  • The while loop ran while the variable num was less than or equal to 10.
  • The until loop runs until the variable num becomes greater than 10.

Both are different ways of doing the same thing. While is more popular as you'll find a while loop equivalent in most programming languages.

🏋️ Exercise time

That was fun. Time to do some exercise now.

Exercise 1: Write a script that takes a number as an argument and prints its table. Your script should also show a message if the script is run without an argument.

Expected output:

$: ./table.sh
You forgot to enter a number

$: ./table.sh 3
3
6
9
12
15
18
21
24
27
30

Exercise 2: Write a script that lists all the files in the directory /var

Hint: Use for loop with /var/* as the 'list'.

You can discuss your answers in this dedicated thread in the Community:

Practice Exercise in Bash Basics Series #8: For, While and Until Loops
If you are following the Bash Basics series on It’s FOSS, you can submit and discuss the answers to the exercise at the end of the chapter: Fellow experienced members are encouraged to provide their feedback to new members. Do note that there could be more than one answer to a given problem.
Bash Basics Series #8: For, While and Until Loops

The bash basics series is coming to an end. As the final chapter in the series, you'll learn to use functions in bash scripting next week. Stay tuned.



from It's FOSS https://ift.tt/Gc89qA1
via IFTTT

Sabtu, 29 Juli 2023

Getting Started With Linux Terminal

Getting Started With Linux Terminal

The Linux terminal could be intimidating. The dark screen with just commands to use. It's easy to feel lost.

The thing is that Linux command line is a vast topic. You can manage the entire system using just the commands. I mean that's the role of sysadmins, network engineers and many other jobs.

The aim of this tutorial collection is not to make you job-ready. It intends to give you the starting point of your Linux command line journey. It will give you enough to navigate the terminal and understand a few basic things like reading files and editing them.

Since it's just the beginning, most of the tutorials are in the 'file operation' category. That's where most Linux books and courses begin.

📋
The best way to learn is by doing it yourself. I have written the tutorials in 'hands-on mode' so you can follow the examples on your Linux system. Each chapter in the series contains some sample exercises to practice your learning. Follow, practice and you'll be getting better at Linux command line in no time.

Chapter 0: Get familiar with the terminal and terminology

When you are absolutely new to the terminal, you'll find yourself lost even while reading the tutorials. That's because you may know always understand even the simplest of terms.

The article below will help you with some of that. While some of the tips I shared may be a little advance or not-so-useful for you at this moment, you'll find plenty of useful stuff.

19 Basic But Essential Linux Terminal Tips You Must Know
Learn some small, basic but often ignored things about the terminal. With the small tips, you should be able to use the terminal with slightly more efficiency.
Getting Started With Linux Terminal

Chapter 1: Changing directories

In the first chapter, learn to switch directories (folders) using absolute and relative paths. This way, you can navigate in the Linux command line.

Changing Directories in Linux Terminal
Learn how to change directories in the Linux command line using absolute and relative paths in this part of the Terminal Basics series.
Getting Started With Linux Terminal

Chapter 2: Making directories

Now that you know about switching directories, learn about creating new ones.

Making Directories in Linux Terminal
Learn to make new folders in the Linux command line in this part of the Terminal Basics tutorial series.
Getting Started With Linux Terminal

Chapter 3: List what's inside a directory

You are getting a good grasp of the directories. Learn to see inside directories and see what files and subdirectories do they have.

Listing the Contents of Directory With ls Command in Linux
In this chapter of Terminal Basics series, learn about displaying the contents of a directory, sorting them and checking file stats.
Getting Started With Linux Terminal

Chapter 4: Creating files

Enough about directories. Learn to create new files in the Linux command line.

Create New Files in Linux Terminal
In this chapter of Linux Terminal Basics series for beginners, learn about creating new files using Linux commands.
Getting Started With Linux Terminal

Chapter 5: Reading files

What's inside the file? Learn to read text files in this chapter.

View the File Contents in Linux Command Line
In this chapter of the Terminal Basics series, you’ll learn about viewing the contents of files in the Linux command line.
Getting Started With Linux Terminal

Chapter 6: Deleting files and directories

Now that you have learned to create new files and folders, it's time to delete them.

Delete Files and Folders in Linux Command Line
You have learned to create files and directories. Now it is time to learn about deleting files and folders in the command line.
Getting Started With Linux Terminal

Chapter 7: Copying files and directories

Keep on with the file operations and learn to copy files and directories in this installment of the terminal basics series.

Copy Files and Directories in Linux Command Line
Learn how to copy files and directories in Linux using the command line in this part of the Terminal Basics series.
Getting Started With Linux Terminal

Chapter 8: Moving files and directories

Moving file operation is like cut-paste. You can use the same method for renaming files and directories as well.

Linux Terminal Basics #8: Move Files and Folders
In the eighth chapter of the Terminal Basics series, learn about moving files and directories using the mv command in Linux.
Getting Started With Linux Terminal

Chapter 9: Editing files

As the last major file operation, learn to edit text files in the command line.

Linux Terminal Basics #9: Editing Files in Linux Terminal
Learn about editing text files in the Linux terminal using the beginner friendly Nano editor in the second last chapter of this series.
Getting Started With Linux Terminal

Chapter 10: Getting help

Now that you have learned plenty of the basic Linux command line operation, it's time to know how you can get help in the terminal itself.

Linux Terminal Basics #10: Getting Help in Linux Terminal
Learn how you can get help about using Linux commands in the final chapter of the Terminal Basics series.
Getting Started With Linux Terminal

Where to go from here?

Now that you are more comfortable with the terminal and know the basic file operations in the command line, you may wonder what comes next.

I will suggest getting a Linux book like How Linux Works. However, you can start with any Linux book you come across. Here are a few Linux books I love.

Best Linux Books For Beginners to Advanced Linux Users
Here are some Linux book recommendations to improve your knowledge. These books cater to the need of beginners and experts and help you master Linux concepts.
Getting Started With Linux Terminal

Don't want to spend money on books yet? No worries! Here are some free Linux ebooks you can download.

20 Best Linux Books You Can Download For Free Legally
Let me share the best resource to learn Linux for free. This is a collection of Linux PDFs that you can download for free to learn Linux.
Getting Started With Linux Terminal

Bash scripting is also an integral part of Linux learning. Even if you don't have to write shell scripts, if you know the basics, you should be able to understand scripts you come across while using Linux.

Learn Bash Scripting For Free With This Tutorial Series
New to bash? Start learning bash scripting with this series in am organized manner. Each chapter also includes sample exercises to practice your learning.
Getting Started With Linux Terminal

There is no limit to learning. It's impossible to know it all. Yet, if you know at least the basics, it helps you in using the system more effectively.

🗨 I hope you like this Linux terminal tutorial series. Do share your feedback in the comments section.



from It's FOSS https://ift.tt/kJ0rYBj
via IFTTT

Understanding Ubuntu’s Repository System

Understanding Ubuntu’s Repository System

When you start using Ubuntu or an Ubuntu-based distribution, soon you’ll find yourself using apt commands to install software like this:

sudo apt install package_name

You’ll probably also install additional software using the 3-step PPA command:

sudo add-apt-repository ppa:PPA_Name/ppa
sudo apt update
sudo apt install package_from_ppa

And when you start adding random external repositories, you’ll encounter update errors sooner or later.

Now, you can search for the error on the internet and perhaps fix it as well. Most of the time, you won’t understand what causes the problem and how it was fixed.

But if you understand the repository mechanism in Ubuntu, you’ll understand the root cause of the problem and fix it accordingly.

You might find all this information in fragments on It’s FOSS and various forums. But the fragmented information is not easy to understand.

Hence, I created this page to give you a comprehensive understanding of how the repository system works.

This guide also benefits the users of Linux Mint, elementary OS and other distributions based on Ubuntu (and perhaps Debian).

📋
This is intended for end users, not developers. I will not show you how to package an application for Ubuntu. You’ll get the necessary knowledge for maintaining your system’s repository sources.

Let's start with the essentials first. Understands the basic concept of the package manager and the repositories. This will build a foundation for you to understand how things work underneath.

Chapter 1: What is a Package Manager?

In simpler words, a package manager is a tool that allows users to install, remove, upgrade, configure and manage software packages on an operating system.

Ubuntu provides the software through repositories. The repositories contain software packages of different kinds. They also have metadata files that contain information about the packages such as the name of the package, version number, description of package and the repository name etc.

The package manager interacts with the metadata and creates a local metadata cache on your system. When you ask it to install a software, the package manager refers to this cache to get the information and then uses the internet to connect to the appropriate repository and downloads the package first before installing it on your system.

Understanding Ubuntu’s Repository System

This article explains the working of the package manager in a bit more detail. Do check it out.

What is a Package Manager in Linux?
Learn about packaging system and package managers in Linux. You’ll learn how do they work and what kind of package managers available.
Understanding Ubuntu’s Repository System

Chapter 2: Ubuntu’s Default Repositories

From the previous section, you have some idea of the repositories.

You may have guessed that there is not a single repository that contains all the packages. Why not? Because it is categorized to have the packages in different repositories so that they can be enabled or disabled accordingly.

Each Ubuntu version has its own official set of four repositories:

  • Main – Canonical-supported free and open-source software.
  • Universe – Community-maintained free and open-source software.
  • Restricted – Proprietary drivers for devices.
  • Multiverse – Software restricted by copyright or legal issues.
How to Enable Universe and Multiverse Repositories in Ubuntu
This detailed article tells you about various repositories like universe, multiverse in Ubuntu and how to enable or disable them.
Understanding Ubuntu’s Repository System

Chapter 3: Understanding sources.list

The sources.list is an integral part of Ubuntu's repository mechanism. Several update-related issues find their root cause in the incorrect entries in the sources.list file.

This is why it is important to understand it.

Sources.list is basically a text file that contains the repository details. Each uncommented line represents a separate repository.

Understanding Ubuntu’s Repository System

But these entries follow a specific format and should not have duplicate entries. Read more about them in the article below.

What is the Use of sources.list File in Ubuntu Linux?
Understanding the concept of sources.list in Ubuntu will help you understand and fix common update errors in Ubuntu.
Understanding Ubuntu’s Repository System

Chapter 3: Going in-depth with PPA

Though Snaps are pushed by Ubuntu a lot these days, PPAs are still a reliable way to get newer software in Ubuntu.

Many Ubuntu users blindly run three commands to use the PPA and naturally end up with issues like 'repository does not have release file'.

This is an in-depth and one-of-a-kind guide on PPA. It will improve your knowledge tremendously.

Understanding PPA in Ubuntu Linux [Ultimate Guide]
An in-depth article that covers almost all the questions around using PPA in Ubuntu and other Linux distributions.
Understanding Ubuntu’s Repository System

Adding External Repositories

Apart from the default repositories and PPAs, you will also add software from their party repositories. Softwares like Brave browser, Docker and many more provide dedicated repositories for Ubuntu.

Understand the mechanism of using external repositories.

Installing Packages From External Repositories in Ubuntu
Learn the logic behind each step you have to follow for adding an external repository in Ubuntu and installing packages from it.
Understanding Ubuntu’s Repository System

Troubleshooting Common Errors

Now that you are familiar with the underlying mechanism, it's time to get familiar with the typical update errors you might encounter while using Ubuntu.

When you see go through the error, you may start getting the hint about the root cause. And when you go through the root cause, you will understand how the specific solution fixes it.

And that's the aim of this page. To give you enough under-the-hood knowledge on the package management system so that you can avoid common errors or effectively fix them.

You'll understand the why along with the how.

Failed to Download Repository Information

If you encounter this error, it's just the beginning of your investigation.

Understanding Ubuntu’s Repository System

While the internet connection could be the issue in some cases, chances are that you have an issue because of some repository you added or wrong entries in the sources.list.

You should jump into the terminal and (try to) refresh the package cache.

sudo apt update

Look at the last few lines of the output. It will give you the error message (lines starting with E:). When you have the error message, your troubleshooting begins.

Repository Does Not Have a Release File

A common error Ubuntu users encounter while blindly adding PPAs to the system without checking if the PPA exists for their Ubuntu version or not.

E: The repository ‘http://ppa.launchpad.net/numix/ppa/ubuntu focal Release’ does not have a Release file.

Let me give you a hint. The repository is not configured for the Ubuntu version being used.

What to do When You See “Repository does not have a release file” Error in Ubuntu
One of the several ways of installing software in Ubuntu is by using PPA or adding third-party repositories. A few magical lines give you easy access to a software or its newer version that is not available by default in Ubuntu. All thing looks well and good until you get
Understanding Ubuntu’s Repository System

Problem With MergeList

If you see an error like this:

E:Encountered a section with no Package: header,
E:Problem with MergeList /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_precise_universe_binary-i386_Packages,
E:The package lists or status file could not be parsed or opened.

It indicates that somehow the cached file mentioned in the second line got corrupted. The fix is to empty the cache and repopulate it.

How To Fix Ubuntu Update Error: Problem With MergeList
One of the most common error a user encounters while updating is Problem with MergeList. The error could be encountered while using both Ubuntu Update Manager and using the sudo apt-get update in terminal. The complete error looks something like this: E:Encountered a section with no Packag…
Understanding Ubuntu’s Repository System

Target Packages is configured multiple times

Alright! Technically, not an error. It's just a warning. But it is still quite common and could trouble many new Ubuntu users.

This is a good example for putting your newly acquired knowledge here.

There are duplicate entries in the sources.list files and that's causing the issue. The solution is to delete all the duplicate lines keeping just one.

[Solved] Target Packages is configured multiple times Error
Seeing repository configured multiple times error while updating your system with apt update command? Here’s what you can do about it.
Understanding Ubuntu’s Repository System

Continue Learning

I hope you have a slightly better understanding of the repository mechanism in Ubuntu. The next time you encounter an error and come across a solution, you are more likely to understand what's going on.

Next, you should learn to use the apt command for package management. There's more than just apt install or apt remove.

Using apt Commands in Linux [Ultimate Guide]
This guide shows you how to use apt commands in Linux with examples so that you can manage packages effectively.
Understanding Ubuntu’s Repository System

You may also go a bit into detail about package management.

How To Install And Remove Software In Ubuntu [Complete Guide]
Brief: This detailed guide shows you various ways to install applications on Ubuntu Linux, and it also demonstrates how to remove installed software from Ubuntu. When you switch to Linux, the experience could be overwhelming at the start. Even basic things like installing applications on Ubuntu…
Understanding Ubuntu’s Repository System

Since Ubuntu is pushing for Snaps more than ever, learn the essential Snap commands.

Using Snap Packages In Ubuntu & Other Linux [Complete Guide]
Ubuntu’s new universal package Snaps are now everywhere. You should learn the essential snap commands to master this new packaging system.
Understanding Ubuntu’s Repository System

Flatpak applications are also gaining popularity and you may come across them sooner or later.

Install and Use Flatpak on Ubuntu
Ubuntu may come with Snap by default but you could still enjoy the Flatpak universal packages on it.
Understanding Ubuntu’s Repository System

💬 I hope you enjoyed learning about the repository mechanism and has more clarity on package management. Do share your thoughts in the comment section.



from It's FOSS https://ift.tt/AvW5kx0
via IFTTT