Covert bash script to zsh.

I have just upgraded from Mojave to Big Sur.

Need some script advice.


I have a script for bash. Need some help to convert this to zsh.


bash version :

#!/usr/bin/env bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users:/Users/root:/Users/root/Scripts:/Library/Scripts:/Library/Scripts/Startup
#Load modules for Fuse
/Library/Filesystems/macfuse.fs/Contents/Resources/load_macfuse
/usr/sbin/sysctl -w vfs.generic.macfuse.tunables.allow_other=1
#Connect to rsync_net
/bin/sleep 45
/usr/local/bin/sshfs XXXXX@XXXX.ch-s011.rsync.net: /Volumes/rsync.net -olocal,auto_cache,reconnect,volname=rsync.net,allow_other,defer_permissions,async_read


And here is another one that I would like to convert :

I have the following in my .bash_profile :

#Setting PATH for meson and ninja
export PATH=$PATH:/Users/tormod/Library/Python/3.9/bin
#Setting PATH for Python 3.9
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH


What is the corresponding for a zsh profile ? .zprofile ?







Posted on Nov 20, 2021 3:11 AM

Reply
18 replies

Nov 20, 2021 5:14 AM in response to tormod_bjorøy

You should have a ~/.zshrc which will be read when you launch a new Terminal, or switch between Bash and Zsh. No need for a ~/.zprofile dotfile.


Just switch the following:


#!/usr/bin/env bash

to:


#!/bin/zsh


and run the scripts to test.


My ~/.zshrc (substantially reduced) looks like this:


export PATH=".:$HOME/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/opt/homebrew/bin:${PATH}"
export HISTFILE=~/.zsh_history
export HISTFILESIZE=10000
export HISTSIZE=10000
setopt HIST_IGNORE_SPACE
setopt INC_APPEND_HISTORY
# don't let VPN DNS servers change the displayed hostname in the prompt
MyHost="$(networksetup -getcomputername)"
PS1="${MyHost}: %~ %% "
# use vi/vim for command line edits
bindkey -v
# because regular history command is a very short list
alias hist='history 1 | more'
alias hist_reset='history -p'
# grep history for a string
hgrep () { fc -Dlim "*$@" 1 }


It is a good idea to put all other Zsh functions in ~/.zshenv.




Nov 20, 2021 7:32 AM in response to VikingOSX

Thanks !


I will test this ! One question :

I have used the

/bin/sleep 45


This commad was used in order for the script to wait a little so that the internet connection should get some time to get up and running. This script is to be run at boot time.

Is there another command I can use to check if there is an active internet connection WIFI or LAN ( a little more complicated )?

Nov 21, 2021 1:41 PM in response to VikingOSX

I got my ip adress running

ifconfig -l | xargs -n1 ipconfig getifaddr

as per your suggestion


I also get my IP adress using a command like this :

scutil --nwi

( Reachable 0x00000002 / Not Reachable 0x00000000 )


I am a beginner in editing / writing scrips.


Will your script run again and again untill an IP adress has been found ?

Or is it possible to have the script run every x seconds ?


What I'm trying to achieve is that the command

/usr/local/bin/sshfs XXXXX@XXXX.ch-s011.rsync.net: /Volumes/rsync.net -olocal,auto_ca.....

should only be run if I have an IP adress = Internet connection

( This script is only run at boot time )


So internet connection = true, then i run

/usr/local/bin/sshfs XXXXX@XXXX.ch-s011.rsync.net: /Volumes/rsync.net -olocal,auto_ca.....


I would be grateful for a suggestion that can achive this.


Nov 22, 2021 5:49 AM in response to tormod_bjorøy

The while/done block will run until an IP address is obtained in the myip variable. You could place a sleep command prior to the the existing assignment in that loop, and then place the sshfs syntax immediately after that while/done block as at that point an IP address is available.


Another thought would be this one liner instead of my original while/done block:


while [ $(scutil -r XXXX.ch-s011.rsync.net) != "Reachable" ]; do done;


This will loop until the remote host is Reachable and then end the loop.

Nov 22, 2021 7:30 AM in response to VikingOSX

FYI:

scutil -r other-home-Mac.local 

says it is "Reachable", even when I'm VPN'ed into work, where the work VPN client blocks all .local traffic


ping -c 1 -t 3 other-home-Mac.local

is not as fast as scutil -r, but it does error out when I'm VPN'ed into work, which is what some of my scripts want to know.


Knowing this I can still get to other-home-Mac, but I have to Rube Goldberg it, by tunneling through the corporate firewall and accessing other-home-Mac via the Internet and a high numbered port forwarded through my home router.

Nov 22, 2021 4:15 PM in response to VikingOSX

Yes, this is a laptop - a MacBook Pro only. I think this will work.

I have a com.startup.plist file. This file is run as a launchDaemon at boot time.

The com.startup.plist initializes running of my startup.sh script

My startup.sh script is the script in the question. This has now been changed from bash to zsh.


The script only connects to cloud storage rsync.net.

The script is only run at boot time. I have another script to run "anytime", if the boot script fails....


The sshfs command connects to cloud storage rsync.net and mounts the cloud to /Volumes/rsync.net


The sshfs command WILL FAIL if i do not have an internet connection, that is why I would like to know that i have internet connected BEFORE I run the sshfs command.


The startup script at boot works now, I only considered some fine adjustment...


I have the possibility to connect to a VPN from my laptop, but I always connect to my VPN manually.

And if i will use VPN, then this will happen AFTER the internet to the macbook is up and running, and after the sshfs command in the script is run.


If I have an IP adress on the laptop, or is "reachable" than it is ok to run the long sshfs command ( I hope )



So If I am going to use the The while/done block....

Can I do like this :


#Load modules for Fuse
/Library/Filesystems/macfuse.fs/Contents/Resources/load_macfuse
/usr/sbin/sysctl -w vfs.generic.macfuse.tunables.allow_other=1
#Connect to rsync_net
myip=0
while [ $myip = 0 ]
do
  sleep 3
  myip=$(ifconfig -l | xargs -n1 ipconfig getifaddr)
done
/usr/local/bin/sshfs XXXXX@XXXX.ch-s011.rsync.net: /Volumes/rsync.net -olocal,auto_cache,reconnect,volname=rsync.net,allow_other,defer_permissions,async_read


So the loop will pause 3 seconds before it starts again, and when it finds ip adress, it will run sshfs command ?



Another challenge: How can I do an umount command at shutdown of the laptop ?

sudo umount /Volumes/rsync.net


If the mount is not properly unmounted before shutdown, than the sshfs command will fail at boot.....


Nov 29, 2021 2:44 PM in response to VikingOSX

Ok I tested the script at boot time.


This works :

myip=0while [ $myip = 0 ]
do
/bin/sleep 8
myip=$(ifconfig -l | xargs -n1 ipconfig getifaddr)
done
/usr/local/bin/sshfs XXXXX@XXXX.ch-s011.rsync.net: /Volumes/rsync.net -olocal,auto_cache,reconnect,volname=rsync.net,allow_other,defer_permissions,async_read



This does NOT work :


myip=0while [ $myip = 0 ]
do
/bin/sleep 6
myip=$(ifconfig -l | xargs -n1 ipconfig getifaddr)
done
/usr/local/bin/sshfs XXXXX@XXXX.ch-s011.rsync.net: /Volumes/rsync.net -olocal,auto_cache,reconnect,volname=rsync.net,allow_other,defer_permissions,async_read



I presume the OS needs some seconds to load something.

Sleep 6 : does not work

Sleep 7 : works

Sleep 8 : works

Dec 12, 2021 9:23 AM in response to tormod_bjorøy

myip=0
while [ $myip = 0 ]

not

myip=0while [ $myip = 0 ]


Is it possible to change the script, so that the sshfs command will only run AFTER the ifconfig command has run in a loop several times ?


The while loop only runs as long as it takes for your local IP to get configured and active. The sshfs command follows the IP loop when an IP address is assigned and the loop condition becomes false.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Covert bash script to zsh.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.