AU
  
You are currently viewing the Australia version of the site.
Would you like to switch to your local site?
13 MIN READ TIME

LXF SHELL

The LXF Shell in… the redirection redemption

Ferenc Deák continues writing shell-enhancing redirection features and hiding the odd film title for you to spot, the little scamp!

OUR EXPERT

Ferenc Deák is sure that the usual suspects of programming languages fit for writing a shell have been exhausted, so he sticks to C++.

QUICK TIP

The code for the LXF Shell can still be found at https://github. com/fritzone/lxf-shell.

Previously in our LXF Shell series, we reached a stage P where we could execute applications (LXF310), and looked at how to properly redirect the output of an application (LXF310). For this latest instalment, we are planning how to implement the input redirection for the shell, and as a final touch, how to bring all these together and create a shell that can sequentially execute applications by piping input and output between them.

Input redirection

Linux input redirection in a shell enables you to change the source of input for a command or program. It is accomplished with the < operator, by adding < to redirect the standard input (stdin) of a command or program from a file instead of the keyboard after the command, and specifying a filename whose content is read and used as input for the command to be executed. For example: wc -l < input.txt .

In this example, wc -l reads its input from the input. txt file instead of waiting for keyboard input, and counts the lines in that file. Input redirection can be particularly useful when working with scripts and batch processing, because it enables you to automate tasks and process large volumes of data without manual input.

From a programming point of view, input redirection happens in a similar way to output redirection, using pipes, as the following program exemplifies. To save precious space, we have omitted the error checking; however, the example code found at https://github. com/fritzone/lxf-shell has all the necessary checks and comments, so be sure to check it out. int main() { int pipe_fd[2] = {-1, -1};

This diagram represents how pipe interactions can happen in Linux.

Part Three! Don’t miss next issue, subscribe on page 16! pipe(pipe_fd); pid_t child_pid = fork(); if (child_pid == 0) { close(pipe_fd[1]); dup2(pipe_fd[0], STDIN_FILENO); close(pipe_fd[0]); char program_name[] = “wc”, *args[] = {program_ name, NULL}; execvp(program_name, args);

} else { close(pipe_fd[0]);

FILE *input_file = fopen(“input.txt”, “r”); char buffer[4096] = {0}; size_t bytesRead = 0; while ((bytesRead = fread(buffer, 1, sizeof(buffer), input_file)) > 0) write(pipe_fd[1], buffer, bytesRead); close(pipe_fd[1]); wait(NULL);

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for $1.48
SUBSCRIBE NOW
30 day trial, then just $14.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
March 2024
VIEW IN STORE

Other Articles in this Issue


Linux Format
codeclub
www.codeclub.org.uk
Linux Format
The #1 open source mag Future Publishing Limited,
WELCOME
MEET THE TEAM
This issue, we’re looking at exciting new distributions, so what new distro have you tried recently that you would recommend readers try for themselves?
Cutting edges
Living in the open source world means if
REGULARS AT A GLANCE
Linux to save Windows 10 PCs from the apocalypse?
240 million PCs could be scrapped after October 2025, when free support ends for Windows 10. Or users could just switch to Linux.
Cosmic looking out of this world
Pop!_OS Cosmic DE is to be released as an alpha, with a new terminal and overhauled graphics support.
Chromium Wayland accelerated!
VA-API now works on Linux Ozone/Wayland platform.
NEW YEAR RELEASE!
Italo Vignoli is one of the founders of
DATABASE BASES
David Stokes is a technology evangelist at Percona.
Unity bans VLC plugin
Unity management making friends and influencing people.
OpenWrt router?
OpenWrt floats the idea of its own hardware router.
Man writes kernel in Rust
Maestro is a Unix-like kernel written from scratch in Rust.
Distro watch
What’s behind the free software sofa?
WILY WESTON
Marius Vlad is a senior software engineer at
REISER’S GUILT
Jon Masters is a kernel hacker who’s been
Kernel Watch
Jon Masters keeps up with all the latest happenings in the Linux kernel, so you don’t have to.
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to answers@linuxformat.com
Mailserver
WRITE TO US Do you have a burning
HotPicks
Shotwell YouTube Downloader Plus Blue Recorder EncryptPad Buttercup Resources Rnote heXon EDuke32 Delta Chat Proton VPN
OLD-SCHOOL HACKING 101
CREDIT: Magictorch LXF313 will be on sale Tuesday
REVIEWS
Intel Core i7 14700K
Jacob Ridley thinks this is a perfectly good stand-in for a Core i9.
RlxOS 2023.11
Nate Drake finds himself washed up on the rock of ages that is RlxOS , only to discover that it still needs work.
Emmabuntüs 5
Nate Drake finds that despite being hard to pronounce, the name is the only complicated thing about this Debian-based distro.
Manjaro 23.1 Vulcan
Nate Drake boldly goes to take Vulcan for a test spin and finds an incredibly user-friendly and efficient operating system.
ROUNDUP
Password managers
RoboForm Keeper NordPass KeePass 2 Bitwarden
THE 5 BEST NEW DISTROS
THE 5 BEST NEW DISTROS
Blast off into the future with Matt Holder as he explores the technology powering a new generation of Linux distributions.
Pi USER
Let them eat lots of Pi!
Hundreds of thousands of Raspberry Pis are hitting the market, with production ramped to the max!
ProPico
Better by design.
Cyberdeck
Survive anything!
PEERLESS PIN NUMBERS
Les Pounder works with groups such as the
Official Pi 5 Case
Can the new official Raspberry Pi 5 case keep the Pi 5 cool? Les Pounder is – brace yourselves readers – on the case…
Geniatech XPI 3566 Zero
All brains and no brawn applies equally to this SBC and to Les Pounder.
Start using MQTT for sensor logs
Les Pounder is a sensitive soul and his new office needs sensors to record data. He’d better break out the maker kit!
IN DEPTH
Orange Pi vs Raspberry Pi
Tam Hanna compares the highest-end single-board computer offerings from Shenzhen Xunlong and the Raspberry Pi Foundation to find out which one is best suited for you.
TUTORIALS
Monitor systems in style
Shashank Sharma doesn’t look dapper in his quickly fading imitation leather jacket, but his terminal is always a sight to behold.
Secure and private comms made easy
Nick Peers takes a fresh look at an old favourite – Jami, a tool that aims to cover all your communication and sharing needs.
Make a Mealie of it
David Rutland dons a tall hat to prepare you a portion of Mealie – a sophisticated recipe management server with a side of meal planning.
Rescue and back up your retro media
Christian Cawley has some old Commodore 64 5.25-inch disks, and wants to revisit some personal files from 40 years ago.
Create NPCs and their conversations
Nate Drake invites you to relive the glory days of point-and-click adventure games by creating your very own.
Emulate an analogue computer digitally
In our continuing journey into analogue computing, Mike Bedford looks at another emulation approach, and how to get a real analogue computer.
The wire to faster Ethernet
Searching for faster than Gigabit Ethernet, the ever frugal Neil Mohr does that compromise thing his ex told him about.
ADMINISTERIA
Do Docker like an adult!
Stuart Burns is as mad as hell and he’s not going to stand for your shoddy Docker deployments any longer!
Starlink
Wherever he may roam, Nate Drake can enjoy fast internet at a fair price.
Hatch
An easy-to-use builder that empowers Mirza Bahic’s creative expression.
PROTECT YOUR SYSTEMS
Davey Winder reveals the biggest threats that hit systems in 2023 and what’s likely to happen over 2024.
CODING ACADEMY
Write and run C64-style 6502 code
David Bolton shows how to get started writing 6502 code and provides a useful example that sorts numbers.
Chat
X
Pocketmags Support