Some good bash and bash alias tricks.

Share your own howto's etc. Not for support questions!

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2020-09-24 02:48

Okay, well of course wanted to add something, errrr actually screwing around with links2 a cli web browser and had to test out posting. Thing is actually kinda cool, well depending on if this post works.

If it does, some random stuff about those thunar aliases, use those in a keybind, rather than having it open in /home, spend much more time in a data partition and it's nice having thunar open at that location in the filesystem instead. Another quick navigation trick thunar in all its file manager awesomeness, just hit Control + L and type out where you wish to go, even helpfully will offer auto-complete to get a user there faster. Of course am sure its a common feature in tons of graphical file managers, still thunar is good stuff. :P

If this works, suggest folks give links2 a spin, its interesting, does allow mouse click too even though of course is bound to take some getting used to. Someone is fed up with the horrid bloat of modern web browsers, why not play with a cli browser ? :)

Trying out edit: Hoas will be so proud, lmao. Yep, fricken thing is cool. Tip: The pg up-down seems to really help in sorta scrolling pgs, though likely a better way too. Also hitting hte Esc key pops up a handy menu thing. Haven't dorked with this in a long time, the tiny amount of system overhead it has is wild. Okay, end test. :)
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby bester69 » 2020-12-05 21:07

To shorthen long paths
:P
Code: Select all
alias bashorten='PROMPT_DIRTRIM=1'
Last edited by bester69 on 2020-12-06 04:25, edited 1 time in total.
bester69 wrote:You wont change my mind when I know Im right, Im not an ...
User avatar
bester69
 
Posts: 1823
Joined: 2015-04-02 13:15

Re: Some good bash and bash alias tricks.

Postby RU55EL » 2020-12-06 02:00

bester69 wrote:To shorthen long paths
:P
Code: Select all
alias bashorten='PROMPT_DIRTRIM=1


I think you mean:
Code: Select all
alias bashorten='PROMPT_DIRTRIM=1'
User avatar
RU55EL
 
Posts: 525
Joined: 2014-04-07 03:42
Location: /home/russel

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-04 06:16

Here's a fun one for the new year ... Been wanting to put together a script to handle chrooting an OS-install for awhile, so decided to dork with it and got a working one setup to my satisfaction, so sharing the outline here ...

Okay someone would need to create a file in their users /home directory, name it test.sh, make it executable with the standard "chmod +x test.sh" or however you want to do that ie: If you wanted to do so in a file manager to give it executable permissions. Then here's the contents ...

Code: Select all
#!/bin/bash

echo # these are ugly as feck but put them in to give newlines to improve readability.
echo "Enter the drive and partition to target ... ie: sda1, sdc2 etc."
read DRIVE # This saves the value the user enters to a variable called DRIVE.
echo
echo
echo "Enter the type of filesystem on the partition ... ie: ext2, ext3 or 4."
read FST # short for filesystem type here. :)
echo
echo "Confirm this is the correct drive, partition and filesystem type before proceeding ... Target is $DRIVE ... which is formatted as $FST ?"
read -p "Proceed with live chroot of $DRIVE, press y to continue. " -n 1 -r
sleep 1s && echo

if

[[ $REPLY =~ ^[Yy]$ ]]
then
sleep 1s &&
echo "YOU ASKED FOR IT BUDDY, now you're gonna GET IT!!!! :o<"
sleep 1s &&
echo
echo "HAPPY NEW YEAR!!! :o)"

fi

exit 0


Save it, open a terminal and type in ". test.sh" to run it. What it does, asks the user to input the drive and partition, then saves the value they enter in a variable called DRIVE, then asks for the filesystem type and does the same, saves the value that's entered into a variable called FST, then offers a prompt ... if someone hits any key other than Y/y it just exits the script but if y or Y is entered it runs the rest of it. In the above there ends up echo'ing You asked for it, etc ... HAPPY NEW YEAR to the screen. :D

Okay time for the working script, handles ext2-3 and 4. Really don't need to even bother with asking for the specific filesystem type, mount would be smart enough to handle it without it but wanted to add it anyway, so did ... Also in the case of someone who has a separate /boot partition(which I don't) that would have to be mounted too, in order to do something like reinstalling grub, same for situations with a GPT system, which has a ESP (efi partition on it, again I don't so didn't bother, this is an old bios-mbr laptop.) Anyway, confirmed it working on several partitions on my old beastie.

Code: Select all
#!/bin/bash

echo # these are ugly as feck but put them in to give newlines to improve readability.
echo "Enter the drive and partition to target ... ie: sda1, sdc2 etc."
read DRIVE
echo
echo
echo "Enter the type of filesystem on the partition ... ie: ext2, ext3 or 4."
read FST # short for filesystem type here. :)
echo "Confirm this is the correct drive, partition and filesystem type before proceeding ... Target is $DRIVE ... which is formatted as $FST ?"
read -p "Proceed with live chroot of $DRIVE, press y to continue. " -n 1 -r
sleep 1s && echo

if

[[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo "Don't forget to exit with Ctrl + D and run cleanup.sh to unmount everything cleanly after leaving the chroot."
sleep 2s &&
sudo mkdir -p /mnt/chroot
sudo mount -t $FST /dev/$DRIVE /mnt/chroot
sudo mount -t proc none /mnt/chroot/proc
sudo mount -o bind /dev /mnt/chroot/dev
sudo mount -o bind /dev /mnt/chroot/dev/pts
sudo mount -o bind /sys /mnt/chroot/sys
sudo cp /etc/resolv.conf /mnt/chroot/etc/ # Live session needs to be online for this to matter but won't hurt anyway.
sudo chroot /mnt/chroot

fi

exit 0


Couple things to point out there, I'm using this from a live session, modified a live ISO, so I could add custom keybinds, bash aliases and scripts to it like the above. Am triggering the thing with one of those bash aliases I set in .bash_aliases. Point I'm wanting to make there is if someone were using this in another context = like on an actual bare metal install, they wouldn't need to recreate the mount point directory everytime it were used, so obviously get rid of the line if using it for that purpose, this one ....

"mkdir -p /mnt/chroot", no need to create an existing mount pt over and over. Am going to use this on bare-metal installs too. If someone is multibooting on a system can be useful to be able to just quickly chroot other installs on that system should need arise. Also as the script mentions, would go ahead and get in the habit of exiting a chroot properly, Ctrl + D and here's the commands to unmount everything cleanly, again I just went ahead and scripted it, so that when the script's run just goes through that series of commands, here's the cleanup.sh contents too.

Code: Select all
#!/bin/bash

sudo umount -l /mnt/chroot/dev/pts
sudo umount -l /mnt/chroot/dev
sudo umount -l /mnt/chroot/proc
sudo umount -l /mnt/chroot/sys

exit 0


Really see no harm in getting into the practice of going through the above and cleanly unmounting everything, on a live session chroot really no biggie but again on bare-metal feel better running through it. Purpose of this is if you do need to chroot an install, it's tedious running through all the cmds to do so, same for all the cmds to unmount and this puppy makes it a snap.

Another naggler to be aware of, as noted in the script too, the live session OS actually needs to be online, connected to the internet for that line to work/matter, if not you'll just end up with an empty file in /etc/resolv.conf which I just found out, as noted, tested the chroot.script on my bare-metal installs. One of which was on sda1, went to go online and kept getting "can't find sever, blahblahblah" in my browser. Took a min to think to check the resolv.conf file and had to replace it with a working one to get that OS back online. Just want to note it as something to be aware of.

Other than that, it's good to go, if I want to chroot one of the Nix Os's on a pc. Fire up live session from the usb and trigger the chroot.script to run, it asks me which drive and partition, what filesystem type, then asks me to confirm by pressing y = yes and if I do so, then runs all the cmds to chroot that OS and reminds me to cleanly exit the chroot and cleanup, unmount everything afterwards.

Anyway, this shares enough of the idea if anyone cares to play with or expand upon it and that's a wrap. Happy new year fellow nixers. :D
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Head_on_a_Stick » 2021-01-04 08:26

Simpler alternative that provides a menu listing the available partitions (call the script with sudo or run it as root):
Code: Select all
#!/bin/bash

_list_part()
{
   # list all partitions except swap & those already mounted
   lsblk -lno name,type,mountpoint | awk '/part/&&!/\//&&!/SWAP/{print $1}'
}

_select_part ()
{
   readarray -t lines < <(_list_part)
   
   local PS3="Choice: "
   echo "Please select the target root partition:"
   select choice in "${lines[@]}"; do
      if [[ -n "$choice" ]]; then
         break
      else
         echo "Invalid choice, please try again." >&2
         continue
      fi
   done
}

_chroot ()
{
   mount /dev/"$choice" /mnt
   arch-chroot /mnt mount -a
   arch-chroot /mnt
   umount -R /mnt
}

_confirm ()
{
   while true; do
      echo "Target partition is /dev/${choice}, it will be mounted on confirmation."
      read -re -p "Is this correct? [Y/n] "
      case "${REPLY^^}" in
         N|NO)
            break
            ;;
         ''|Y|YES)
            _chroot
            exit 0
            ;;
         *)
            echo "Unknown response, please either confirm or deny." >&2
            ;;
      esac
   done
}

main ()
{
   while true; do
      _select_part
      _confirm
   done
}

main

That uses arch-chroot(1) (from the arch-install-scripts package) to bind mount the API filesystems, copy over /etc/resolv.conf and tear down the bind mounts after exiting.

Deb-fan wrote:in the case of someone who has a separate /boot partition(which I don't) that would have to be mounted too

The "mount -a" line mounts all partitions listed in /etc/fstab in the target root partition.

Deb-fan wrote:"mkdir -p /mnt/chroot", no need to create an existing mount pt over and over.

/mnt is intended for temporary mount points so why not just use that directly?

EDIT: refactored script to use functions.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 13273
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-04 11:38

Hey, cool ... all good suggestions so thanks Head_on. Was hoping some folks would improve on the idea. Been advocating that this type of thing would make a good addition to hybrid/live media and finally got around to playing with it myself. Could just be a nice touch to add to live media as part of rescue tool-kit or tool on the ISO. Definitely has tons of room for improvement or expansion ...

Are already polished tools to make reinstalling a borked bootloader easy and/or automagic, adding things to detect or automount an ESP or separate /boot wouldn't be overly difficult ( that trick you/@Head_on posted looks good) and same should be said for dealing with encryption in this mess if the user happens to be one of those nixers who encrypts everything.

For myself, when I modded the ISO, created permanent/predictable mount points on it, so don't need to create /mnt/chroot or use those lines in the chroot.script. Still BIG +1 on why not just use /mnt ... hadn't thought of that ... Though was playing with a couple other refinements of this thing and went ahead and tried them out so want to share findings on it.

The thing about copying resolv.conf from the live session to one of my bare-metal installs crapping things up, decided to go with this to handle it.

Code: Select all
sudo cp /mnt/chroot/etc/resolv.conf /mnt/chroot/etc/resolv.conf.bak # Make a backup of targets resolv.conf if present.
sudo cp /etc/resolv.conf /mnt/chroot/etc/ # Live session needs to be online for this to matter but won't hurt anyway.


Just added that line to the script to make a backup of any resolv.conf file that's on the target partition and rename it resolv.conf.bak. So it'd be easier to restore the thing afterwards ... Also ended up tweaking the cleanup.sh script a bit too, its contents are now as follows ...

Code: Select all
#!/bin/bash

sudo umount -l /mnt/chroot/dev/pts
sudo umount -l /mnt/chroot/dev
sudo umount -l /mnt/chroot/proc
sudo umount -l /mnt/chroot/sys
echo "Enter the drive and partition to unmount now ... Check your typing."
read UNMT && echo "preparing to unmount $UNMT"
read -p "Press y to continue. " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then

sudo umount /dev/$UNMT

fi

exit 0


Added another dialogue to it, so that the script asks which drive/partition should be unmounted. I was talking about unmount everything cleanly, blahblahblah and forgot to include unmounting the actual target itself. :P So in the above there, it asks which drive-partition that is, saves what's entered to the variable UNMT and again used the Y/y prompt so a user can confirm it, otherwise again ... the script just exits.

Can see a lot of potentially useful junk that could be integrated into this type of thing but don't have all the needed hardware on hand to test everything, honestly am not the best qualified to sort all of it out either. Plus all this works perfectly as is for me personally, don't want to try too hard in even attempting to make everything imaginable work for every configuration people may have or want. It's a good idea overall, got things working for me, proof of concept. Though again Head_on thanks for the tips and input on this, good stuff. :)
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Head_on_a_Stick » 2021-01-04 11:54

Deb-fan wrote:the script asks which drive/partition should be unmounted

The man page does not recommend that approach:
umount(8) wrote:Giving the special device on which the file system lives may also
work, but is obsolete, mainly because it will fail in case this device was
mounted on more than one directory.

Use the actual mountpoint in the filesystem instead.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 13273
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-04 12:21

^ Say again ... in mere mortal speak this time please Hoasinator ? ... Kidding :), don't see the harm in it, at least not in this context but I could be missing something too. You're talking to a guy who didn't think of using /mnt for all of this, that really is fraggin brilliant. You've probably mentioned it elsewhere but didn't recall it. All this dang time I'd been typing out huge full paths for junk like this and /mnt was sitting there laughing at me the whole while !!!!

DAM U MNT !!!

Do appreciate the tips and feedback. Hoping whomever will adopt it and polish the concept up, add ncurses menu's and automagicness to the thing or whatever else. May very well be existing tools of this type, haven't really checked around so wouldn't be surprised, regardless think it's useful and hobbled together a working solution for personal use. Now shared some of what I discovered while dorking with it so my work here is done and it's time to do some more critical work ... like watch a movie. :D
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-04 18:27

Was thinking further about this nonsense and came up with the following brainfart on it to share.

REALLY COOL: One downside to this chroot.sh and cleanup.sh affair is having to bother with separate scripts for them which require user interaction. Meaning user has to enter the target drive + partition in both, getting confirmation etc. Which hey is still cool but not uber-l33t as is befitting of meself, LOL ... anyway here's an idea to make the magic happen without that, what if as part of the chroot.sh it creates a file and stores(echo's)the value of the $DRIVE variable to it ie: sda1 or sba3 or whatever else,then the cleanup.sh could be setup to read that and apply it, still echo confirmation to user of which it is just for the sake of it and do the Y/y thing. (or not ... could make it automagic too I guess.)

Lines in chroot.sh would look along the lines of.

sudo touch /path/to/storage.file # To create the thing and then.
echo $DRIVE > /path/to/storage.file # To echo the value of $DRIVE to it.

And cleanup.sh as follows in the appropriate place.(top of script)the rest would just be as shown if were wanting to keep the confirmation deal vs just proceeding seamlessly, atm dunno which to prefer ...

Code: Select all
#!/bin/bash

DRIVE=$(cat /path/to/storage.file)

sudo umount -l /mnt/chroot/dev/pts
sudo umount -l /mnt/chroot/dev
sudo umount -l /mnt/chroot/proc
sudo umount -l /mnt/chroot/sys
echo "Preparing to unmount $DRIVE"
read -p "Press y to continue. " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then

sudo umount /dev/$DRIVE

fi

exit 0
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Head_on_a_Stick » 2021-01-04 18:37

Deb-fan wrote:Hoping whomever will adopt it and polish the concept up, add ncurses menu's

Here's a version that uses whiptail for a similar effect:
Code: Select all
#!/bin/sh

_list_part ()
{
   # list all partitions except swap & those already mounted
   lsblk -lno name,type,mountpoint | awk '/part/&&!/\//&&!/SWAP/{print $1,$1}'
}

_select_part ()
{
   # shellcheck disable=SC2046 # word spitting is needed for the _list_part function
   choice=$(whiptail --notags --menu "Please select target root partition:" 0 0 0 $(_list_part) 3>&2 2>&1 1>&3)
   if [ -z "$choice" ]; then
      exit 1
   fi
}

_chroot ()
{
   mount /dev/"$choice" /mnt
   arch-chroot /mnt mount -a
   arch-chroot /mnt
   umount -R /mnt
}

_confirm ()
{
   if whiptail --yesno "Target partition is /dev/${choice}, is this correct?" 0 0 0; then
      _chroot
      exit 0
   fi
}

main ()
{
   while true; do
      _select_part
      _confirm
   done
}

main

It's slightly off-topic for this thread because I've swapped from bash to POSIX sh, hope you don't mind.

Scrot or it never ran:

Image

:)
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 13273
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-04 18:56

^ No definitely welcome and that looks pretty cool. :D Any all such ideas you or anyone else want to introduce on this topic are encouraged.

Ye gawds bunch of other junk is possible too. Looping the dang chroot.sh or putting it to sleep and then nagging the person to confirm and run the part in the cleanup.sh or if they were to hit anything other than Y/y loop again and bug them again a few mins later, lmao ...

Also really wanting to tackle the damn separate boot and/or if it's dealing with an encrypted partition thing but DO NOT want to dump the time into it, as I don't ever setup a separate boot nor use whole disk encryption either. So it'd just be investing time for the sake of it at this point. Could wipe-out one of my thumb-drives and set up a boot + encrypted install on it but it'd be a real PITA to restore the junk I've got on the only usb drive I have that could be used for this crap. :(

Kinda hoping someone who already has the appropriate setup or a system more prone to experimenting might pitch in on it a bit. This old thing doesn't even support virtualization and I'm not installing Virtualbox and going through all of that for the sake of something I don't have much use for anyway.

Either way I hitherfor... or it might be hitherto .. approve of the inter-scripting communication idea as outlined in that post above, could use that type of thing for a bunch of interesting stuff. :D
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-14 16:34

Cooked up another fun one, of course kept dorking with the chroot.sh idea and it's coming along(errrr mostly/sorta :) )and this is a technique planning to integrate into the project. Using while to loop a script.

Create a file in your users home directory, I named mine trigger.file ... Next up create the script for this, named the one I used loop.sh and make it executable "chmod +x loop.sh" or however you want to set it to be executable ...

Code: Select all
#!/bin/bash

while [ -e /home/yourusername/trigger.file ]; do

sleep 2

done

touch /home/yourusername/yepworked.file


Explanation of that, when while is used as long as the condition is true, the script keeps running whatever commands are set. In the above there that condition is for as long as the file named trigger.file exists ( the -e /path/to/filename part in it)at the specified location /home/username/trigger.file then it's true and the script runs the commands, in the above, the command is sleep 2 (go to sleep for 2secs)then it runs again, will keep doing so until the condition is false or the terminal is closed etc. You could use whatever series of commands or whatever else you like there of course. Anyway when I delete trigger.file, then the condition is no longer true, trigger.file no longer exists so the condition is false and the script runs the next commands, which is to create a file named yepworked.file in your users home directory in this script.

To run the script.

Could type out the full path to it in terminal ie: /home/yourusername/loop.sh or just ". ~/loop.sh" the "~" tilde in that is the same as typing out /home/yourusername or pop open a terminal ( which your users home should already be the working directory when you open new terminals) and type "source loop.sh" or again just ". loop.sh" the .(dot) is just like typing source. Simple, cool and weird ... also fun. Am going to use this to execute another script within 2secs of a certain file being removed. For that I only have to use the path to the script in place of touch /etc/etc/yepworked.file.


Still dorking with it but a cool addition to this, kind of amusing to background the process. Just add an "&" to it ... ". loop.sh &" so the terminal's command prompt is returned and it'll keep running in background. Have more dorking yet to do and quality time to spend with Google until coming up with something that fits what I'm after. Though hopefully this is an amusing bit of shellery that might come in handy for you. :D
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-14 19:57

Not so fast ! Here's another amusing idea that could actually have interesting applications. The self destructing script.

Create an example script, named it imoutofhere.sh make it executable as normal and here's the contents.

Code: Select all
#!/bin/bash

touch /home/yourusername/YAkilledME!
echo "gee thanks dude/tte!" > /home/yourusername/YAkilledME!
rm /home/yourusername/imoutofhere.sh


Then run it. :) Idea I'm trying to point out here is yeppers if someone were to want to run a script and have it remove itself as it's final act of valor ... Then just include the command to do that as the last line in it, as shown above. Note: Keep a copy of these to play with ie: imoutofhere.sh.bak cause these babies are going fast, errrr or you'll end up having to type them out again anyway. :D
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-16 16:03

Hi and welcome to a new episode of dorking with bash scripts. :D

I have a thrilling and could be useful script/trick for people. You'll have to install the package espeak from the Debian repositories for it to work. There's also a newer version of the thing "espeak-ng" which stands for next generation I believe. I'm using plain ole espeak though. Was messing around with some simple ways to setup notifications, can definitely come in handy. Will be cooking something, set the stove timer and then wander into my room where I may promptly forget about it, then may or not hear the annoying beeps from the timer when it goes off. Since I'm getting old, helps to be able to set a quick reminder and have it interupt the movie or whatever I'm doing and remind me chow's almost done. :)

Per usual, create a file somewhere, I chose my users home directory and named it msg.sh, make it executable per usual again "chmod +x msg.sh" Here's the contents of the script.

Code: Select all
#!/bin/bash

read -p " # Please enter amount of time until notification, 15s = 15secs, 10m = 10 minutes etc. Read \"man sleep\" (the manual page for the sleep command ) to see possible time values you can use here. " TIME
echo
read -p " # Enter your notification message, some text to remind youself of what you wanted to do. " MSG # The message espeak will recite.

sleep $TIME && espeak "$MSG" &exit


Self explanatory but that's what it does, you run the script in terminal, it asks how long you want to set the wait time until notification and what you want the computer to say, enter this info, the terminal closes and WHAMO ... However long later it'll do it.

Edit: I recommend using the following message content to test this out.

You are the most attractive and intelligent specimen of humanhood I have ever seen ... WOW, just WOW


Awwww shucks computer, you say the nicest ( and truest) things. Thanks, I'm sort of fond of you too. Who's a good widdle debianbox, you are computer, yes you are, why yes you are. :P
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

Re: Some good bash and bash alias tricks.

Postby Deb-fan » 2021-01-18 19:46

Okay yeppers here we go again, this one goes out to @CwF and right now inclined to think his username stands for Come-on-man w-haaaaa de-F**k .. :D

Anyway, you-@CwF introduced an interesting idea in a trainwreck of a how-to exchange. The autologin + startx without a display manager madness. That being I hadn't considered that it could be used in the way he applied it, logging in multiple users on different tty's(gettys.) automatically. Do think it's a cool idea and so started dorking around and here's some of the results of said dorking ...

I switched over myself to the full method employed in the Gentoo wiki, works great, not that it's technically superior in any way to how it's advised in the Archwiki, nor the bastardized version of the topic I put up originally, which was a mixture of ripping off the Gentoo wiki and some snagged and tested line to automatically startx in .profile. Did so mostly to be different. From everything I've ever seen and have played with all of them, all work fine, can even mix + match the things, all accomplish the same thing. Autologin/startx no display manager.

At this point ... then thought while I'm being different why not endeavor to be even more different. Removed the line to startx from my users .profile file and cooked this nonsense up. Created a script in my usual preferred place, which is still /home/myusername/.bin/startx.sh , yep ... named it startx.sh. Make the thing executable with the usual "chmod +x startx.sh" and here's the contents of it.

Code: Select all
#!/bin/bash

echo
echo
read -n1 -p " To startx press 1 or any key to continue with command-line session. " XORG

if [[ $XORG =~ ^[1]$ ]]; then
    [ "$(tty)" = "/dev/tty1" ] && exec startx
fi

echo
echo


Note: The above there is for tty1, if you were trying to autologin your user or other user accts you've setup on different/other tty's on your system, then yes ... change that as needed.

Added this line to the .profile file in my users home directory. To run a script when my user is logged into the OS. Just add it to the bottom of the file or you can wedge it in somewhere else, really doesn't matter as long as you don't screw up any of the other contents of the file, kay ? Don't copy and paste pieces over other crap in the file, delete stuff from it etc so forth. Anyway added the below to .profile.

Code: Select all
# Running a script at user login.
/home/myusername/.bin/startx.sh # That's the path to the dang script.


Which is what this does, when I boot-up the OS, my user is automatically logged into tty1 and when the .profile file gets sourced it runs that script. What it does is show me that dialogue in the script, the Press 1 to startx etc etc. If I press the number 1 key, that's what it does, starts Openbox for me, if I press any other key, keeps going in a command-line session, at any point I could just type "startx" to launch Openbox or another windows manager or desktop etc. So I got this all setup, rebooted and joy to the world, was greeted with my script asking me to press 1 if I wanted to startx, which I did, Openbox fired up and thought okay, now were/I'm getting somewhere, heel yeah! Opened a terminal typed "openbox --exit" to kill the X session and ? Nope, normal user prompt, because it's not a damn login shell, my wonderous bashy goodness didn't run and at this point decided screw this, it's time to whip out the big hammer.

Created a script at the following location, do this as root or with sudo privileges and I named it logout.sh, /usr/local/bin/logout.sh and added it to a file I have in /etc/sudoers.d/myfile I've discussed this before, very likely in this thread if not in one of the other monsters so not covering it again. If anyone is left with any doubts do not hesitate to enlist the help of google on the topic of how to add a script to a file in sudoers.d so that someone can run sudo commands + scripts-etc with sudo commands without having to enter a password.

That's what I did, in the file I added the path to the script /usr/local/bin/logout.sh at that point I can run that script using "sudo /usr/local/bin/logout.sh" in keybinds, panel launchers, terminals, menu entries etc etc without having to enter a password. Here's the contents of the logout.sh script.

Code: Select all
#!/bin/bash

pkill -KILL -u myusername



What the above does is kill off my user, smashes all processes, logs my user off and because things are setup to automatically log my user into that tty = tty1 here, as described in either of the autologin/startx no display manager how-to's found in tips/tricks section, my user is re-logged back into tty1 and I'm greeted with a fresh login shell and my beautiful scripty friend asking me to press 1 for X or any key for etc. In my case I'm using the windows manager Openbox and so added a custom keybind(key-combo)to run the script when pressed. Any and every Desktop or WM on planet earth should have an easy way to define custom keyboard shortcuts = keybinds, as noted using "sudo /usr/local/bin/logout.sh" in plenty of other stuff or all of them can also be used to run the script too, no password necessary. Here's the Openbox rc.xml file entry I'm using to run the script, when I press Alt + F9.

Code: Select all
    <keybind key="A-F9">
      <action name="Execute">
        <startupnotify>
          <enabled>true</enabled>
          <name>force-logout</name>
        </startupnotify>
        <command>sudo /usr/local/bin/logout.sh</command>
      </action>
    </keybind>


So basically I've taken the automatically startx part out of things, clearly very simple to put it back, in my case I'll just remove the comments = # these things from in front of the lines to startx in the .profile file in users /home. Pretty much all this was done for the heel of it and think it's pretty cool, shrugs. Not much difference, if I want to log into a graphical desktop-etc I have to press one key at boot and there you have it folks. :D

FOOTNOTES: Other observations about this, if you did have it setup so that your or whatever other users are automatically logged into whichever tty's on the system, going further ... If you wanted to have a bunch of GUI apps run when that user starts X. Could just add those as .desktop files (in @CwF's case he's wanting to launch web-browser), could also run additional scripts this way too or by adding whatever apps or scripts you want to run when user starts X in Openbox or Fluxbox's autostart files. Other desktops should have similar functionality but what's outlined ... using .desktop files would work fine too. In others words just using the same methods to autostart or run stuff as a person would in any graphical session.

Another dorkish thing was thinking on this. If someone were going for super-secret, as follows but to me it looks ugly/sloppy. Just leaving the prompt empty, so someone only gets a flashing prompt. Rather than giving instructions on press this or that.

Code: Select all
#!/bin/bash

read  -n1 -p " " XORG

if [[ $XORG =~ ^[1]$ ]]; then
    [ "$(tty)" = "/dev/tty1" ] && exec startx
fi

Last edited by Deb-fan on 2021-01-19 23:36, edited 5 times in total.
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 1009
Joined: 2012-08-14 12:27

PreviousNext

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable