Other What's the go-to scripting language to learn for general purpose-scripting on freebsd?

You are not wrong here, but the easier thing to do is create/keep a "pkg prime-list".
Example (very short) based on what I do. I prefer tcsh as my shell, WindowMaker as my graphical environment, emacs, DarkTable, Gimp, xpdf, claws-mail.
So my "prime-list" for this means I would do:
pkg install tcsh windowmaker gimp emacs darktable gimp xpdf claws-mail

If you keep your home directory separate from a system keep your primelist in a file, then just using pkg and base install you recreate your preferred.

Heck it's very useful when you are creating a new system. Take your existing system, run pkg prime-list > mypackages.lst put that on a USB drive install your new system then "for I in `cat mypackages.lst`; do pkg install $I ; done"
That's generally what I do, but IIRC, that breaks rather a lot if you're using custom compile options for ports. For packages, metaports don't make much sense unless they're intended for use by the masses, as in one of the metaports included in the port tree.

Personally, if I'm going to go the route of using a list to install my pkgs from a file list, I'd just pipe the entire thing to xargs and run pkg like that, but to each his own.
 
That's generally what I do, but IIRC, that breaks rather a lot if you're using custom compile options for ports. For packages, metaports don't make much sense unless they're intended for use by the masses, as in one of the metaports included in the port tree.

Personally, if I'm going to go the route of using a list to install my pkgs from a file list, I'd just pipe the entire thing to xargs and run pkg like that, but to each his own.
Either way works fine. Good point if you are building your own ports, but to me that would assume you have a complete pkg build environment and all your systems point internally not externally so should pick up all your custom options
 
Thanks.

You can also check earlier Ghost in the Shell series articles - they also contain a lot of information useful for scripting.
Where do you place your scripts? My first useful shell script will be configuring my phone as my network interface which i was manually doing everyday.
I'm also thinking about either i use a crontab or just excute my script evreytime i login in ~/.login, i'm leaning more towards ~/login although i do a crontab on reboot.
 
Thanks.

You can also check earlier Ghost in the Shell series articles - they also contain a lot of information useful for scripting.
  1. Where do you put your scripts?
  2. how to run commands with sudo in scripts when there's no one to enter the password? because some time perhaps that script is scheduled.
  3. Would a crontab for user root without sudo in the script would be counted as lazy or there's no official way to do it ?
My Problem:
  • i configure my phone to be my lan network interface evereytime i login, So in ~./login i'm thinking about placing a script to capture and verify the output of ifconfig -lwhich usually returns lo0 ue0 if my phone is connected through usb, To do that i'm thinking about doing a string compartion to see if ue0 is captured from the stdout to confirm that the kernel sees my network interface.
  • After that i'll configure my interface to use dhcp using sudo dhclient ue0. I'm not sure how i'll verifiy if dhclient finished with success or not but it never fails when ue0 is up
  • Last operation is pinging the freebsd.org server and if received packets back that would echoe success.
 
configuring network interfaces is usually done via rc.conf. Although this being a phone, I suspect it is plugged in and removed on the fly - so maybe triggering a script via a devd config file is more suitable.
I run scripts via devd rules on several systems to set up USB audio devices. Using the 'DETACH' type you can also run another script to adjust configurations back to a proper network interface.
 
Aside from the typical shell scripting languages, PERL and python merit mention. For a couple of generations PERL was the sysadmin's scripting language of choice. In more recent history it has been surpassed by python. I've got strong opinions about both languages but python edges out in front with its "easier to digest" syntax. I end up doing a lot of rapid application prototyping with python since it has bindings to about every FOSS library or toolkit out there...and if there is need to get professional then I rewrite in C++ after prototyping in python.
 
configuring network interfaces is usually done via rc.conf. Although this being a phone, I suspect it is plugged in and removed on the fly - so maybe triggering a script via a devd config file is more suitable.
I run scripts via devd rules on several systems to set up USB audio devices. Using the 'DETACH' type you can also run another script to adjust configurations back to a proper network interface.
devd's manual mentions my exact situtation :
  1. For example, devd
might execute dhclient(8) when an Ethernet adapter is added to the
system, and kill the dhclient(8) instance when the same adapter is
removed.
I'm still trying to figure out how to do it .
 
devd's manual mentions my exact situtation :
  1. For example, devd
might execute dhclient(8) when an Ethernet adapter is added to the
system, and kill the dhclient(8) instance when the same adapter is
removed.
I'm still trying to figure out how to do it .
have a look at the examples in devd.conf()
'action' can also just call generic shell scripts, that's how I usually use it.
 
My Problem:
  • i configure my phone to be my lan network interface evereytime i login, So in ~./login i'm thinking about placing a script to capture and verify the output of ifconfig -lwhich usually returns lo0 ue0 if my phone is connected through usb, To do that i'm thinking about doing a string compartion to see if ue0 is captured from the stdout to confirm that the kernel sees my network interface.
  • After that i'll configure my interface to use dhcp using sudo dhclient ue0. I'm not sure how i'll verifiy if dhclient finished with success or not but it never fails when ue0 is up
  • Last operation is pinging the freebsd.org server and if received packets back that would echoe success.

Check 'Smartphone USB Tethering' section from here:
- https://vermaden.wordpress.com/2022/09/14/freebsd-cope-with-wifi-fuckup/

Create /usr/local/etc/devd/usb_tethering.conf with this:

Code:
attach 0 {
  device-name "ue[0-9]+";
  action "/etc/usb_tethering.sh start $device-name";
};

detach 0 {
  device-name "ue[0-9]+";
  action "/etc/usb_tethering.sh start $device-name";
};

Use this to 'sniff' devd(8) events live:

Code:
# nc -U /var/run/devd.pipe

Create /etc/usb_tethering.sh script with 'start' and 'stop' sections to configure/unconfigure the `ue0` interface for You.

... as You will be attaching a phone to your laptop - you may as well lock/unlock the desktop with it:
- https://vermaden.wordpress.com/2020...-configuration-unlock-your-laptop-with-phone/


Where do you put your scripts?

For me at ~/scripts and for 'root' at usually /root/bin place.

how to run commands with sudo in scripts when there's no one to enter the password? because some time perhaps that script is scheduled.

Configure sudo(8) od doas(1) to NO ask for password for specified tasks - examples below.

Code:
# cat /usr/local/etc/sudoers
  %network ALL = NOPASSWD: /etc/rc.d/netif onerestart
  %network ALL = NOPASSWD: /etc/rc.d/routing onerestart
  %network ALL = NOPASSWD: /sbin/dhclient *
  %network ALL = NOPASSWD: /sbin/ifconfig *
  %network ALL = NOPASSWD: /sbin/ifconfig * up
  %network ALL = NOPASSWD: /sbin/route *
  %network ALL = NOPASSWD: /sbin/umount -f *
  %network ALL = NOPASSWD: /usr/bin/killall -9 dhclient
  %network ALL = NOPASSWD: /usr/bin/killall -9 ppp
  %network ALL = NOPASSWD: /usr/bin/killall -9 wpa_supplicant
  %network ALL = NOPASSWD: /usr/bin/killall *
  %network ALL = NOPASSWD: /usr/bin/tee -a /etc/resolv.conf
  %network ALL = NOPASSWD: /usr/bin/tee /etc/resolv.conf
  %network ALL = NOPASSWD: /usr/local/sbin/vm switch address *
  %network ALL = NOPASSWD: /usr/sbin/ppp *
  %network ALL = NOPASSWD: /usr/sbin/service squid onerestart
  %network ALL = NOPASSWD: /usr/sbin/wpa_supplicant *

# cat /usr/local/etc/doas.conf
  permit nopass :network as root cmd /etc/rc.d/netif args onerestart
  permit nopass :network as root cmd /etc/rc.d/routing args onerestart
  permit nopass :network as root cmd /usr/sbin/service args squid onerestart
  permit nopass :network as root cmd dhclient
  permit nopass :network as root cmd ifconfig
  permit nopass :network as root cmd killall
  permit nopass :network as root cmd killall args -9 dhclient
  permit nopass :network as root cmd killall args -9 ppp
  permit nopass :network as root cmd killall args -9 wpa_supplicant
  permit nopass :network as root cmd ppp
  permit nopass :network as root cmd route
  permit nopass :network as root cmd tee args -a /etc/resolv.conf
  permit nopass :network as root cmd tee args /etc/resolv.conf
  permit nopass :network as root cmd umount
  permit nopass :network as root cmd vm args switch address
  permit nopass :network as root cmd wpa_supplicant


Would a crontab for user root without sudo in the script would be counted as lazy or there's no official way to do it ?
No.
 
Check 'Smartphone USB Tethering' section from here:
- https://vermaden.wordpress.com/2022/09/14/freebsd-cope-with-wifi-fuckup/




Configure sudo(8) od doas(1) to NO ask for password for specified tasks - examples below.

Code:
# cat /usr/local/etc/sudoers
  xaS

# cat /usr/local/etc/doas.conf
  permit nopass :network as root cmd /etc/rc.d/netif args onerestart
  permit nopass :network as root cmd /etc/rc.d/routing args onerestart
  permit nopass :network as root cmd /usr/sbin/service args squid onerestart
  permit nopass :network as root cmd dhclient
  permit nopass :network as root cmd ifconfig
  permit nopass :network as root cmd killall
  permit nopass :network as root cmd killall args -9 dhclient
  permit nopass :network as root cmd killall args -9 ppp
  permit nopass :network as root cmd killall args -9 wpa_supplicant
  permit nopass :network as root cmd ppp
  permit nopass :network as root cmd route
  permit nopass :network as root cmd tee args -a /etc/resolv.conf
  permit nopass :network as root cmd tee args /etc/resolv.conf
  permit nopass :network as root cmd umount
  permit nopass :network as root cmd vm args switch address
  permit nopass :network as root cmd wpa_supplicant



No.
  • This sudo tip was so valuable for me, I always found it tiresome typing the same long password for commands that i use a ton on the daily but i realized it will affect my opsec in the long term.

  • My phones has these little annoying widget buttons that i need to shoo away everytime i plug to usb plus it needs to be unlocked once after plugging , it's messing up my devd event causing my script to get executed earlier than i thought because ifconfig does not see my phone as a network interface until i do the latter, i just improvised the best i can.
 
  • My phones has these little annoying widget buttons that i need to shoo away everytime i plug to usb plus it needs to be unlocked once after plugging , it's messing up my devd event causing my script to get executed earlier than i thought because ifconfig does not see my phone as a network interface until i do the latter, i just improvised the best i can.

You can enable 'developer mode' on your Android phone and get some additional options.

For example I have changed/enabled two things on my phone:
- as long as its attached to USB cable - do not lock the screen
- whenever I attach that phone to a computer - the USB Tethering is the DEFAULT mode

Hope that helps.
 
I use Tcl (often with Expect) when I get to choose. I also like Perl.

Python's not my cup of tea. I studied it for over one hundred hours back in 2017; I found it ever more insufferable the further I went. But it's hard to avoid by those with an appetite for paychecks.
 
You can enable 'developer mode' on your Android phone and get some additional options.

For example I have changed/enabled two things on my phone:
- as long as its attached to USB cable - do not lock the screen
- whenever I attach that phone to a computer - the USB Tethering is the DEFAULT mode

Hope that helps.
I had some of those settings you mentioned enabled from the start but only the screen thing... i didn't want that because i can't leave my phone unlocked for how long I'll be sitting on the computer, It will certainly affect the battery life along being it in charge mode through the usb, now i just have to unlock my phone before i plug to usb that's it.
these are the variables i went with after listening with netcat, very simple stuff :
Code:
notify 0 {
        match "system"          "IFNET";
        match "subsystem"       "ue0";
        action                  "dhclient ue0";
};
attach 0 {
        match "system"          "IFNET";
        match "subsystem"       "ue0";
        action                  "dhclient ue0";
};
 
That's not my recollection other than in couple of very specific spots. That's looking back to Fortran 77 anyhow.
"yaml" (gitlab ci files are in this language).
indentation is important. Spaces don't mix with "tab chars".

Fortran? I don't recall because last time I used it was on punch cards. 80 column not the imposter 96 cols
 
And it's not even avant-garde - indent-as-syntax goes back to fortran.
Not in any Fortran I know. Here is the 77 and 90 syntax:
Code:
if (some condition) then
   true statement
else 
    false statement
end if

if (another condition) statement
The second one is the single-line version of if, for the case when you need to only execute one statement.

Note that the above syntax is actually very close to the C-style syntax, if you replace "then" with "{", "end if" with "}", and else with "} else {". And that just like Fortran, C allows single-statement if statements.

The use of whitespace as indentation is indeed older than Python, but it was only very rarely used. The only language I know of that had it was Occam, which comes from the CSP logic (communicating sequential processes). Given Theo's origin in a computer science research group in the Netherlands, he must have been quite familiar with CSP and Occam, although I don't know whether Python was in any way inspired by it.

Here is my personal opinion: Code with structures (such as if, while, do ...) has to be indented, because that makes it more readable. And yes, I've worked in enough code in languages that did not use block structures (Fortran-IV, COBOL, RPG-II), and non-indented code is AWFUL to read. Once you have made that decision, having both syntactic block markers (such as "{" and "}", or "then" and "end if") PLUS indentation is redundant. To begin with, that redundancy enables creating broken code, where the indentation and braces disagree. This is awful, because such code may execute correctly, but become unmaintainable. Fortunately, normal build systems have linters built in, and will detect such mistakes.

Now the question becomes: Is that redundancy good or bad? In my opinion, it is just wasteful. It often makes the code longer in terms of lines (meaning less code fits on the screen, increasing cognitive load), because many coding style rules force braces to be on lines by themselves (in traditional fixed-form Fortran, the "else" and "end if" have to be on lines by themselves). It enables silly mistakes (which the linter quickly catches), but that then forces having linters in your build system. Given that we're going to indent anyway and that is mandatory, I vote for making the indentation carry the semantic information about block structure, like Python does.

But honestly, this is not a big deal. I also program in C/C++ (and Java and Rust and ... many other languages with brace syntax). Compared to all the other things a skilled software engineer needs to know, indentation and block bracing is the least of the problems.
 
Fortran 4, and strongly agree that it was awful (fugly) to read.

We used a homemade pre-processor superset that may have emulated 77, but it already existed in '74, so no idea about the origin - it was probably just topnotch design engineering.
 
"yaml" (gitlab ci files are in this language).
indentation is important. Spaces don't mix with "tab chars".
YAML is a perfect example of semantic whitespace gone wrong. There are so many links I could use here. My personal experience with it has been dismal. Yes, I've run into weird number-parsing bugs (you meant to give that one CPU? You just gave it 1,000.) Yes, I've spent hours chasing an indentation error buried deep in a giant file.

Theo's origin in a computer science research group in the Netherlands...
Did you mean Guido? I think you got two people with Dutch-sounding names mixed up.
:)

My personal opinion is things that are invisible should not have meaning in a computer program. Yes, everyone indents their code, but everyone has sometimes strongly-held opinions on the right way to indent. Anyways, the indentation thing is the third-worst thing about Python for me.
 
Fortran 4, and strongly agree that it was awful (fugly) to read.
I used to actually indent Fortran IV source code. It doesn't violate the rules, makes things easier to read. Makes the code narrower, since you only have from column 8 to 72. But back then, we had fewer variables, therefore shorter variable names.

We used a homemade pre-processor superset that may have emulated 77, but it already existed in '74, so no idea about the origin - it was probably just topnotch design engineering.
Was it Mortran? That was a genius language. Terrible hack, hard to debug (because the generated Fortran code was ugly it times), but pleasant to code in. Sort of Fortran IV with modern structured tooling, like good loops.
 
Did you mean Guido? I think you got two people with Dutch-sounding names mixed up.
:)
So sorry! You are absolutely correct, I mean Guido not Theo.

The funny thing is that Guido was the office mate of a good friend of mine in the early 90s; the friend really likes Guido (the person, colleague, and fellow scientist), and hates the language. He kept scripting in Tcl until Ruby came along. Reason? The mandatory whitespace was a minor reason; the big reason was the object model, where "every variable is an object", even within a class (which is one of the reasons the language is so slow, a class object can not rely on knowing its member variables, so everything has to go through the dictionaries), except "primitives are second class objects". I see that as a valid criticism, but I understand the counterargument, which is ease of use: a class object can add and remove members at runtime.
 
YAML is a perfect example of semantic whitespace gone wrong. There are so many links I could use here. My personal experience with it has been dismal. Yes, I've run into weird number-parsing bugs (you meant to give that one CPU? You just gave it 1,000.) Yes, I've spent hours chasing an indentation error buried deep in a giant file.
OT, or maybe not, this reminds me of pkl available in ports and pkgs. For those that don't know, it's from Apple and is intended to be used as something that you can write in that can be used to generate JSON, YAML and a couple other configuration formats. I've been meaning to give it a go, but the only use I have for it right now would be for stuff that I already coded for JSON.

I've barely looked into it, but it looks a bit smarter in terms of avoiding excessive white space or bracketing.
 
Back
Top