简体   繁体   中英

Which process executes script in terminal

when i write ./test1.sh in the MacOS terminal which process executes the script ?

I have installed on my machine oh-my-zsh and thus run zsh in my terminal.

Running test1.sh like so:

./test1.sh

outputs

1 март 2016/ 1 януари 2015/
1 март 2016/
./test3.sh: line 14: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
./test3.sh: line 15: януари: syntax error: invalid arithmetic operator (error token is "?нуари")
blah

while running it like this

zsh test3.sh

outputs

1 март 2016/
1 януари 2015/
blah

On the other hand running

declare -A newarray
newarray[януари]="1"
qn="януари"
echo ${newarray[$qn]}

outputs

1

Why is that ?

test3.sh

# backup IFS
SAVEIFS=$IFS

# set IFS to newline
IFS=$(echo -en "\n\b")

# get files
FILES=$(ls -1 -d */)
echo ${FILES}
IFS='\n'
read dirsNameArray <<< ${FILES}
echo ${dirsNameArray[0]}

declare -A monthMap
monthMap['януари']="1"
# monthMap[февруари]="2"
# monthMap[март]="3"
# monthMap[април]="4"
# monthMap[май]="5"
# monthMap[юни]="6"
# monthMap[юли]="7"
# monthMap[август]="8"
# monthMap[септември]="9"
# monthMap[октомври]="10"
# monthMap[ноември]="11"
# monthMap[декември]="12"


# iterate over files
IFS='\n'
for f in $FILES
do
  echo "blah"
  IFS=' '
  # read -r dirNameArray <<< $f
  # echo "${monthMap[${dirNameArray[2]}]}"
  IFS='\n'
done

# restore $IFS
IFS=$SAVEIFS

You should always include a shebang in your scripts. If you want your shell script to be run by zsh then make sure that the topmost line of you script looks like that:

#!/bin/zsh

This will guarantee that your script will be executed by /bin/zsh (or whatever other executable you specified in the shebang).

If you want to find out what shell is used to execute your script, add the following line to it:

ps ho cmd $$

and see what it prints. If you want to know what shell is used in an interactive session, check if either $BASH_VERSION or $ZSH_VERSION is defined.

Let's find out what shell zsh uses to execute text files:

% echo 'ps -f $$' > script.sh && chmod +x script.sh && ./script.sh
UID        PID   PPID   C STIME TTY      STAT   TIME CMD
slim       17311 16570  0 15:45 pts/0    S+     0:00 sh ./script.sh

So it uses sh . That make sense, sh is the lowest common denominator, default shell.

To force a different shell, use #! in the first line of the text file:

% echo '#!/bin/zsh' > script.sh && echo 'ps -f $$' >> script.sh && chmod +x script.sh && ./script.sh
UID  PID   PPID   C STIME TTY      STAT   TIME CMD
slim 17342 16570  0 15:46 pts/0    S+     0:00 /bin/zsh ./script.sh

#! is a general purpose mechanism, so you can use it to execute pretty much anything that reads from stdin and ignores "comments" beginning with # -- perl, python, most shells, awk, even things like gnuplot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM