linux - Bash script lost shebang path after instantiate function -


i writing script iterative menu run command lines. however, after create iterative menu got error when want run commands. error [command]no such file or directory linux.

#!/bin/bash atests=("test name 1" "teste name 2") path=("test1.xml" "text2.xml")  menu () {    in ${!atests[@]};        printf "%3d%s) %s\n" $((i+1)) "${opt[i]:- }" "${atests[i]}"    done    [[ "$msg" ]] &&  echo "$msg"; : }  prompt="check atest (again uncheck, enter when done): "  while menu && read -rp "$prompt" num && [[ "$num" ]];     /usr/bin/clear;     [[ "$num" != *[![:digit:]]* ]] &&     (( num > 0 && num <= ${#atests[@]} )) ||     { msg="invalid atest: $num"; continue; }     ((num--)); msg="${atests[num]} ${opt[num]:+un}checked"     [[ "${opt[num]}" ]] && opt[num]="" || opt[num]="+" done  in ${!atests[@]};     [[ "${opt[i]}" ]] && { printf "%s " "${atests[i]}"; msg=""; } done  echo "$msg"  in ${!atests[@]};    if [[ "${opt[i]}" ]] && [[ $pwd = /repo/$user/program ]];        find . -iname ${path[i]} -exec cat {} \;    fi done 

i want find *.xml file execute script exist , belong /usr/bin. find command dont execute , cat command in example, getting following error ([command]no such file or directory linux.)

if try run 1 bash command before function, command execute without problem, after function commands fails.

i create 1 alias script running inside /repo/$user/program without include path script.

the problem has nothing shebang or function. problem you're using variable $path. variable tells system directories search executable commands, when set array... it's going start looking commands in locations(s) specified ${path[0]}, "test1.xml", not directory let alone 1 contains of executables need.

solution: don't use variable name path other list of directories search commands. in fact, since 1 of large number of all-uppercase variables have special functions, it's best use lowercase (or mixed-case) variables in scripts, avoid weird conflicts this.

btw, can't tell if rest of script makes sense or not; use of short-circuit booleans conditional execution (e.g. this && || something) makes hard follow logic. i'd recommend using if blocks conditional execution (as did in for loop @ end); make easier tell what's going on.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -