Search code examples
linuxubuntuzsh

How to pass variables through three or more zsh programs?


So I have several scripts that I've made to help myself, but apparently after moving to a different OS (from mint to ubuntu) and installing zsh they stopped working. This is one of my cases called the 'cweb' script which was supposed to let me in a specific directory (through the terminal). But apperantly the $location is not being edited through the whole script therefore I don't move a directory, anyone have a clue? I admit I've made several changes to the codes since then but yet they don't work as I expected them to.

If someone could take a look and help me here..

I open the terminal and I write 'cweb'

cweb
What would you like to run?
Press f to frontend, b to backend. [f/b]
f
Reading frontend directory ('frontend_dir') text file..
Work Directory Location Exported.

Moving onto the chosen directory..
There you go!

So in when I typed in the terminal vim ~/.zshrc I wrote

SCRIPTS_DIR=/home/skillz/Desktop/Desktop_Items/Work/Scripts
export PATH="$PATH:$SCRIPTS_DIR"
export SCRIPTS_DIR=$SCRIPTS_DIR/Supporting_Scripts

Inside the scripts path I have the 'cweb' script which is

cweb:

#!/bin/zsh
$SCRIPTS_DIR/ask_server
echo "Moving onto the chosen directory.."
cd $location
echo "There you go!"
exec zsh

ask_server:

#!/bin/zsh
echo "What would you like to run?"
while true; do
  echo "Press f to frontend, b to backend. [f/b]"
  read -k 1 fb
  echo
  case $fb in
    [Ff]* ) zsh $SCRIPTS_DIR/read_frontend_dir && break;;
    [Bb]* ) zsh $SCRIPTS_DIR/read_backend_dir && break;;
    * ) echo "Unrecognized Option."
  esac
done
echo $location

read_frontend_dir:

#!/bin/zsh
echo "Reading frontend directory ('frontend_dir') text file.."
file=$SCRIPTS_DIR/Dirs/frontend_dir.txt
while IFS= read -r line
do
  location="$line"
done <"$file"
echo "Work Directory Location Exported."

read_backend_dir:

#!/bin/zsh
echo "Reading backend directory ('backend_dir') text file.."
file=$SCRIPTS_DIR/Dirs/backend_dir.txt
while IFS= read -r line
do
  location="$line"
done <"$file"
echo "Work Directory Location Exported."

frontend_dir.txt

/home/skillz/cweb-frontend/

backend_dir.txt

/home/skillz/skillz-coding-website/

TL;DR I run a script called CWEB which prompts me with a question. I can either type f or b, by my desicion it will read a different directory and pass me over to that directory. The script is working fine and all, it gets all the correct scripts to run one by one. but eventually cd-ing to the returned location doesnt work. I printed $location after receiving it and apperantly it is empty.. Any clue?

Anyone has an idea why it doesn't move me to the 'chosen directory'? It does exist and cd there works fine.


Solution

  • Apperantly before calling any script I need to make it save all the variables in zsh, by typing source at the beginning of the code, all the enviroment will be kept between the scripts and it will come to live great again.