Sample Header Ad - 728x90

How to add a substring to a string as a column

2 votes
1 answer
143 views
The title may not be the best at describing the issue, but this was the best I could come up with, moving on to describe what I'm trying to do, I use artix runit as my os, and it is tedious to create a symlink for every service I want to enable, so I created this script to make it easier.
#!/bin/sh

rundir="/run/runit/service"
svdir="/etc/runit/sv"

for SERVICE in $(ls $svdir); do
    [[ -d "$rundir/$SERVICE" ]] && output="$output$SERVICE        linked: yes\n" || output="$output$SERVICE        linked: no\n"
done

service=$(printf "$output" | dmenu -l 10) || exit 1

if printf "$service" | grep -Fqe "linked: yes"; then
    service=$(printf "$service" | awk '{print $1}')
    sudo -A rm "$rundir/$service"
else
    service=$(printf "$service" | awk '{print $1}')
    sudo -A ln -s "$svdir/$service" "$rundir/"
fi
Which checks all the services that can be enabled in /etc/runit/sv and checks if they are already enabled by seeing if they exist in /run/runit/service if it does it will add linked: yes at the end of the dmenu list and if it doesn't, it adds linked: no the problem is that the service's names vary so adding those things will result in this inconsistency: inconsistency as you can see as service's names vary the location of the text at the end change which is not very appealing to the eye I would say, so I would like to ask if there's a solution to this, if there's none I guess I would add the linked: xxx at the beginning since that would solve the problem, but it's my last resort. And if anyone wants to point out a more efficient way to write the code please do, this is my first bash script and I have much to learn. Note: I use dash as sh so i can't use any kind of bashisms.
Asked by Stagnant (33 rep)
Feb 14, 2023, 09:01 PM
Last activity: Feb 15, 2023, 12:14 AM