Tag: Ruby

How to convert a string to lowercase in Bash

This note will show you how to convert a string to lowercase (small letters) on the Linux command line.

To convert a string to lower case regardless of its current case, use one of the following commands.

tr

echo "Hi all" | tr '[:upper:]' '[:lower:]'
hi all

Attention! If you want to change the case of any letters other than Latin (national alphabets, letters with diacritics), then do not use tr, but use any other solution suggested below. This is because the classic Unix tr operates on single-byte characters and is not compatible with Unicode.

AWK

echo "Hi all" | awk '{print tolower($0)}'
hi all

Bash

a="Hi all"
echo "${a,,}"
hi all

Starting with Bash 5.1, there is a conversion option “L”, which is intended to convert a string to lowercase:

${var@L}

Example:

v="heLLo"
echo "${v@L}"
hello

sed

echo "Hi all" | sed -e 's/\(.*\)/\L\1/'
hi all

Or this solution:

echo "Hi all" | sed -e 's/\(.*\)/\L\1/' <<< "$a"
hi all

Another solution:

echo "Hi all" | sed 's/./\L&/g'

Perl

echo "Hi all" | perl -ne 'print lc'
hi all

Python

a="Hi all"
b=`echo "print ('$a'.lower())" | python`; echo $b

Ruby

a="Hi all"
b=`echo "print '$a'.downcase" | ruby`; echo $b

PHP

b=`php -r "print strtolower('$a');"`; echo $b

NodeJS

b=`node -p "\"$a\".toLowerCase()"`; echo $b

In zsh

a="Hi all"
echo $a:l

How to convert a string to uppercase in Bash

This note will show you how to convert a string to upper case (capital letters, uppercase) on the Linux command line.

To convert a string to capital letters regardless of its current case, use one of the following commands.

tr

echo "Hi all" | tr '[:lower:]' '[:upper:]'
HI ALL

Attention! If you want to change the case of any letters other than Latin (national alphabets, letters with diacritics), then do not use tr, but use any other solution suggested below. This is because the classic Unix tr operates on single-byte characters and is not compatible with Unicode.

AWK

echo "Hi all" | awk '{print toupper($0)}'
HI ALL

Bash

a="Hi all"
echo "${a^^}"
HI ALL

Starting with Bash 5.1, there is a conversion option U, which is intended to convert a string to uppercase:

${var@U}

Example:

v="heLLo"
echo "${v@U}"
HELLO

sed

echo "Hi all" | sed -e 's/\(.*\)/\U\1/'
HI ALL

Or this solution:

echo "Hi all" | sed -e 's/\(.*\)/\U\1/' <<< "$a"
HI ALL

Another solution:

echo "Hi all" | sed 's/./\U&/g'

Perl

echo "Hi all" | perl -ne 'print uc'
HI ALL

Python

a="Hi all"
b=`echo "print ('$a'.upper())" | python`; echo $b

Ruby

a="Hi all"
b=`echo "print '$a'.upcase" | ruby`; echo $b

PHP

b=`php -r "print strtoupper('$a');"`; echo $b

NodeJS

b=`node -p "\"$a\".toUpperCase()"`; echo $b

In zsh

a="Hi all"
echo $a:u

Error “ruby-bundler: /usr/share/man/man5/gemfile.5.gz exists in filesystem (owned by ruby)” (SOLVED)

Whenever operating system packages are updated (as well as when new packages are installed), in addition to checking dependencies, package managers also check that there is no file conflict. That is, a package containing files that are already on disk and do not belong to this package will not be updated or installed.

During a regular system update (Arch Linux, for example) with the command

sudo pacman -Syu

you may run into error:

(8/8) checking for file conflicts                  [######################] 100%
(8/8) checking for file conflicts
error: failed to commit transaction (conflicting files)
ruby-bundler: /usr/share/man/man5/gemfile.5.gz exists in filesystem (owned by ruby)
Errors occurred, no packages were upgraded.

This issue is specific to the ruby-bundler-2.2.16-1 package. The essence of the error is that the gemfile.5.gz file already exists in the file system, it belongs to the ruby package, and this file is also present in the new version of the ruby-bundler package. As a result, the update cannot complete due to file conflicts.

Apparently, this problem will be solved in the ruby-3.0.1-1 package, which is currently at the testing stage (the [testing] repository).

You do not have to wait for the upgrade from ruby 2 to ruby 3, especially since this process can be delayed, you can use one of the following workarounds.

Please note that the file /usr/share/man/man5/gemfile.5.gz is just a manual file, documentation, that is, this file is not critical for the operating system.

You can overwrite this file right during the update, for this run the command:

sudo pacman -Syu --overwrite /usr/share/man/man5/gemfile.5.gz

Related article: Analogue of the --force option in pacman

Another option is just delete this file before updating:

sudo rm /usr/share/man/man5/gemfile.5.gz
sudo pacman -Syu

These methods are equivalent, choose any of them to update the packages in the operating system.

How to run “bundle install” as root

The “bundle install” command does not allow you to run it as root. For example, the following sequence of commands will fail:

gem update --system
xcode-select --install
gem install nokogiri
bundle install

The last command will show an error:

Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
installing your bundle as root will break this application for all non-root
users on this machine.
Could not locate Gemfile

This can be a real problem if the primary user on your computer is root. On some servers, the default user is root.

bundle install” has no options to ignore the command being run with elevated privileges. But there is still a way to get around this problem - create a new user and execute the command on his behalf.

To create a new user in Debian, Kali Linux, Linux Mint, Ubuntu, run a command like this:

sudo useradd -m -G sudo -s /bin/bash NEW_USER

To create a new user in Arch Linux, Manjaro, BlackArch and their derivatives, run a command like:

sudo useradd -m -g users -G wheel,video -s /bin/bash NEW_USER

After that, it is enough to sign in as a new user:

su - NEW_USER

And run bundle again:

bundle install

This time the command will end successfully.

To return to the root user, that is, log out of the new user session, press Ctrl+d.

Loading...
X