Press "Enter" to skip to content

Tag: linux

Techy: Changing Python Shebang path from absolute to env

I’m just working in some Python code, and quite a lot of it has the “shebang” line for the Python interpreter set as an absolute path (such as /usr/bin/python) which won’t work for me as I need the scripts to use whichever version of Python I have configured in my “environment”: and the ideal way to do this is to call is using /usr/bin/env python – so how do I change these…

Techy: Microsoft PowerShell and Linux Commands

I’m finding myself mix and matching between GNU/Linux and Microsoft PowerShell quite a bit at the moment and which most Linuxy/Linuxesque (?) commands work fine under PowerShell, some are a little bit different.

Here’s a few which may be handy to know (the only order is that which I entered them). I’ve also added a couple of Windows commands where they are slightly easier to remember/shorter than PowerShell – and, me being me, included links to the appropriate documentation where possible.

Linux CommandPowerShell CommandDescription
which <commandname>
Example:
which composer
GNU Logo GNU “which”
Get-Command <commandname>
Example:
Get-Command composer
Powershell Logo PowerShell “Getting Environment Variables”
Find the location of an executable called <commandname>
In PowerShell, Get-Command can be referred to as just “gcm” so the example would become “gcm composer
printenv
GNU Logo GNU “printenv”
Get-Item -Path Env:
Powershell Logo PowerShell “Getting Environment Variables”
List all environment variables.
PowerShell also supports using “ls” to list environment variables – such as “ls env:
<variablename>="<value>"
Example:
HELLO="you guys"
Set-Item -Path Env:<variablename> "<value>"
Example:
Set-Item -Path Env:HELLO "you guys"
Powershell Logo PowerShell: “Change the value of an environment variable”
Sets an environment variable called <variablename> to the text <value>

PowerShell also supports using “$env:<variablename>="<value>” ” – such as ” $env:HELLO="you guys"
echo $<variablename>
Example:
echo $PATH
GNU Logo GNU “echo”
Get-ChildItem -Path Env:<variablename>
Example:
Get-ChildItem -Path Env:PATH
Powershell Logo PowerShell “Get A Selected Environment Variable”
Show the value of an environment variable called <variablename>
PowerShell also supports using “ls” to list environment variables – such as “ls env:PATH

echo <text>
Example:
echo "Hello there!"

GNU Logo GNU “echo”
Write-Output <text>
Example:
Write-Output "Hello there!"
Powershell Logo PowerShell “Write-Output”
Displays/prints a <text> string to the terminal
grep "<string>" <file>
Example:
grep "sort code" document.txt
GNU Logo GNU “grep”
Select-String "<string>" <file>
Example:
Select-String “sort code” document.txt
Powershell Logo PowerShell “Select-String”
Search a file <file> for the given text <string> using a regular expression

grep "<string>" <file>
Example:
grep "sort code" document.txt
GNU Logo GNU “grep”
findstr /R "<string>" <file>
Example:
findstr /R "sort code" document.txt
Windows LogoWindows Command: “findstr”
Search a file <file> for the given text <string> using a regular expression

ls | grep "<string>"
Example:
ls | grep "\.html"
GNU Logo GNU “ls”
ls | Out-String -Stream | Select-String "<string>"
Example:
ls | Out-String -Stream | Select-String “\.html”
Powershell Logo PowerShell “Select- String: Convert Pipline Objects”
Search a directory listing for a filename containing <string> using a regular expression
ls | grep "<string>"
Example:
ls | grep "\.html"
GNU Logo GNU “ls”
dir | findstr /R "<string>"
Example:
dir | findstr /R "\.html"
Windows LogoWindows Command: “findstr”
Search a directory listing for a filename containing <string> using a regular expression.
wget <url> --output-document <filename>
Example:
wget https://h.tld/f.gif --output-document o.gif
GNU Logo GNU “wget”
Invoke-WebRequest <url> -OutFile <filename>
Example:
Invoke-WebRequest https://h.tld/f.gif -OutFile o.gif
Powershell Logo PowerShell “Invoke-WebRequest”
Save a file at a URL <url> as a local file called <filename>
\
(backslash)
Example:
a \
b
GNU Logo GNU “The Backslash Character”

(backtick)
Example:
a
`
b
Powershell Logo PowerShell “…Line Continuation in Code…”
Allows a command to be split across multiple lines using the multiline separator/line continuation character.
&&
Example:
echo "A" && echo "B"
GNU Logo GNU “Lists
&&
Example:
Write-Output "A "&& Write-Output "B"
Powershell Logo PowerShell “Pipeline Chain Operators”
Command chaining using pipeline operators/list operators – if the condition on the left is true/passes, then continue.
rm <file>
Example:
rm test.tmp
GNU Logo GNU “rm”
Remove-Item <file>
Example:
Remove-Item test.tmp
Powershell Logo PowerShell “Remove-Item”
Deletes a file called <file>
unzip <file>
Example:
unzip myfile.zip
Expand-Archive <file>
Example:
Expand-Archive myfile.zip
Powershell Logo PowerShell “Expand-Archive”
Extracts a .zip archive file
uname
Example:
uname -nrmo
GNU Logo GNU “uname”
(note, the output order of uname in GNU/Linux cannot be altered)
Get-ComputerInfo
Example:
Get-ComputerInfo -Property CsDNSHostName, OsVersion, OsArchitecture, OsName | ConvertTo-Json
Powershell Logo PowerShell “Get- ComputerInfo”
Powershell Logo PowerShell “ConvertTo-Json”
Gets basic information about the system. For example:
Computer’s “hostname”
[GNU:n PS: CsDNSHostName]
OS or kernel release
[GNU: r PS:OsVersion]
Machine/Processor type
[GNU:m PS:OsArchitecture]
Operating system (OS) name
[GNU:o PS: OsName]
These commands were testing on a Debian bullseye Linux virtual machine and using Microsoft PowerShell 7.2 on Windows 10.

Microsoft PowerShell – don’t you mean Windows PowerShell?

Nope.

If you have a Windows 11, 10, 8.1, 8.0 or 7 machine, it would have come “with default” with Windows PowerShell – however, Microsoft has replaced that with a new “multiplatform” version which – in their infinite wisdom – have decided to call Microsoft PowerShell but haven’t “forcibly upgraded” people (you can upgrade yourself via their “Installing PowerShell on Windows” guide).

Microsoft do have a webpage about the differences between the PowerShells if you want to read: but if you use the command line a lot, it might be worth looking at Windows Terminal as well.

Which version of PowerShell do I have?

If you type/copy:

(Get-Host).Version

into the shell, you’ll get:

  • A “Major” version number of 7 or above = (new) Microsoft PowerShell
  • A “Major” version number of 5 or lower = (old) Windows PowerShell
  • A “Major” version number of 6 = I don’t think this actually exists and therefore should be used as the “cut-off” point between the two.
  • “.Version was unexpected at this time” = Probably using Windows Command Prompt
  • “-ash: syntax error: unexpected word” = You are in a shell of a Linux machine!
  • “bash: syntax error unexpected token `.Version’ = Linux again!

Real Life Example

As an example, here’s a single line Linux “command script” which uses curl to fetch a page from https://api.example/latest , parse it for the first occurrence of the text:

"tag_name":"v..."

(where v…. is any sequence of characters starting with “v” and ending with a quote mark) and store that as $VERSION then pass that to curl again to download that specific version zip file from https://example.com/v${VERSION}.zip as “example.zip” (replacing ${VERSION} with the extracted version number), unzip it and then delete the downloaded .zip file.

And, yes, I’ve colour coded the appropriate sections so you can see which command “maps” to which other command.

Linux Example

VERSION=$(
 curl --silent "https://api.example.com/latest" | \
 grep '"tag_name":' | \
 sed -E 's/.*"v([^"]+)".*/\1/' \
) && curl -L https://example.com/v${VERSION}.zip -o example.zip \
&& unzip example.zip && rm example.zip

Windows PowerShell 7 Example

if (((Invoke-WebRequest "https://api.example.com/latest").Content) -match '"tag_name":"v(?<ver>[^"]+)"') {
$VERSION=$Matches.ver &&
Invoke-WebRequest "https://example.com/v${VERSION}.zip" -OutFile example.zip &&
Expand-Archive example.zip && Remove-Item example.zip }

Snippet/Techy: Fixing Debian Signatures were invalid – Key Expired

If you are running Debian/Ubuntu and you are getting “The following signatures are invalid” when running apt-get commands (such as apt-get upgrade) and the error looks like:

Reading package lists... Done
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://repo.mysql.com jessie InRelease: The following signatures were invalid: KEYEXPIRED 1487236823 KEYEXPIRED 1487236823 KEYEXPIRED 1487236823

W: GPG error: http://nginx.org jessie InRelease: The following signatures were invalid: KEYEXPIRED 1471427554
W: Failed to fetch http://repo.mysql.com/apt/debian/dists/jessie/InRelease

W: Some index files failed to download. They have been ignored, or old ones used instead.

As root run:
for K in $(apt-key list | grep expired | cut -d'/' -f2 | cut -d' ' -f1); do sudo apt-key adv --recv-keys --keyserver keys.gnupg.net $K; done

Source: http://serverfault.com/a/615362/12514

[Techy] localdomain settings in Linux Mint using DNSMasq

If you are developing websites on Linux Mint, then you might want to set a DNS Wildcard so that anything on localdomain (*.localdomain) resolves to your machine (i.e. test.localdomain , anything.localdomain). So how can you do this?

First of all, you need to install dnsmasq “a lightweight, easy to configure DNS forwarder and DHCP server”, this can be simply done using:
sudo apt-get install dnsmasq

Now you just need to configure it. Create a file in /etc/dnsmasq.d/ using something like nano, pico, vi, emacs :

sudo nano -w /etc/dnsmasq.d/localdomain.conf

with the following settings:

address=/localdomain/127.0.0.1
listen-address=127.0.0.1

This will tell DNSMasq to setup a wildcard for everything on “localdomain” to point to 127.0.0.1 and to listen for DNS requests on 127.0.0.1. Now just restart DNSmasq:
sudo service dnsmasq restart
and you are nearly done.

You now just need to change your DNS servers in network manager. On the Linux Mint task bar, right click on the network icon and select “Edit connections” and edit the connection you are using. Select “IPv4 Settings”. If you have “Automatic (DHCP)” selected, change it to “Automatic (DHCP Addresses only)”. Then add the DNS server 127.0.0.1 and others of your choosing (such as the Google 8.8.8.8 and 8.8.4.4 ones). All should now be working!

Funny: Funny Linux Commands

Shamelessly stolen from Frank Mash (or, as UK news organisations will probably argue, “this orphaned content found was at …”):

% cat “food in cans”
cat: can’t open food in cans

% nice man woman
No manual entry for woman.

% “How would you rate Quayle’s incompetence?
Unmatched “.

% Unmatched “.
Unmatched “.

% [Where is Jimmy Hoffa?
Missing ].

% ^How did the sex change operation go?^
Modifier failed.

% If I had a ( for every $ the Congress spent, what would I have?
Too many (‘s.

% make love
Make: Don’t know how to make love. Stop.

% sleep with me
bad character

% got a light?
No match.

% man: why did you get a divorce?
man:: Too many arguments.

% !:say, what is saccharine?
Bad substitute.

% %blow
%blow: No such job.

% \(-
(-: Command not found.

$ PATH=pretending! /usr/ucb/which sense
no sense in pretending!

$ drink matter
matter: cannot create

$ ddate
Today is Prickle-Prickle, the 69th day of Chaos in the YOLD 3176

and of course:

unzip ; strip ; touch ; grep ; finger ; mount ; fsck ; more ; yes ; umount ; sleep

Some of these work, some of these don’t – it all depends on your OS version. ddate does work on Centos.