Sign inSign up free

748. Split Paths Without Calling basename

medium 20 XP ~10 minHands-on lab

Write /home/student/bin/pathinfo.sh. For each path given as an argument, it prints one line:

<dir>|<base>|<name>|<ext>

Task

PartMeaning
direverything before the last /, or . when there is no /
baseeverything after the last /
namebase without its extension
extthe part after the last dot of base, without the dot; empty when there is none

A file name that starts with a dot and has no other dot (.bashrc) has no extension. Examples:

$ pathinfo.sh /var/log/app.log.gz report.pdf /etc/.bashrc "dir with space/file"
/var/log|app.log.gz|app.log|gz
.|report.pdf|report|pdf
/etc|.bashrc|.bashrc|
dir with space|file|file|

Use only bash itself to take the paths apart (no basename, dirname, sed, awk or cut).

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

Part of Shell Scripting for Automation. Next step: The Counter That Always Says Zero

Teaching or writing about this? Embed this lab on your page

TerminalNot started

Your machine is ready to start

A fresh, isolated Ubuntu machine boots in a few seconds with the task already set up.

No account needed: 3 of 3 free tries left. Guest machines last 30 minutes.