shelljs-transpiler icon indicating copy to clipboard operation
shelljs-transpiler copied to clipboard

shift function does not work

Open Brassrat opened this issue 6 years ago • 6 comments

The 'shift' function is implemented via exec('shift') - but this doesn't/can't/shouldn't change the process.argv array. thus the value of, e.g., $1 does not change after shift.

Seems that process.argv needs to be copied and $N references are to this copy. 'shift M' functions would replace this copy by a slice of it thus changing the values associated with $K

Brassrat avatar Nov 30 '18 20:11 Brassrat

Is this what you mean?

$ help shift
shift: shift [n]
    Shift positional parameters.

    Rename the positional parameters $N+1,$N+2 ... to $1,$2 ...  If N is
    not given, it is assumed to be 1.

    Exit Status:
    Returns success unless N is negative or greater than $#.

nfischer avatar Dec 02 '18 03:12 nfischer

yes ... I haven't tried other stuff yet, such as various shell variables, i.e., BASH_SOURCE, PWD, RANDOM, PPID, HOME, or the myriad parameter expansion expressions :-) (such as ${N:-xxx} or ${VAR//FROM/TO}

Note - BASH_SOURCE[0] is the abs path to the script (versus $0 which is how the script was invoked)

Brassrat avatar Dec 03 '18 14:12 Brassrat

various shell variables

Shell variables (values set specifically by the shell itself but not exported) probably won't work. Not sure if it makes sense to implement them in ShellJS or here, but I'm open to discussing it.

BASH_SOURCE, PWD, RANDOM, PPID, HOME

Note that some of these are environmental variables, which will work :smile:. Although, apparently PWD is only exported in bash, not zsh.

or the myriad parameter expansion expressions

Some are supported, take a look at #4. Help is appreciated.

nfischer avatar Dec 04 '18 03:12 nfischer

I agree most Shell variables are not relevant. I thought, however, that the ones i mentioned might be of use. BASH_SOURCE[0] is AFAIK the only reliable way to get an absolute path to the folder containing the shell script, which i use sometimes if i need to have additional/optional related files and/or scripts (which within a node app could be handled differently, but in terms of transpiling existing bash code.) Although the full array of sources is probably not needed. And, of course, node provides __dirname (so maybe, BASH_SOURCE[0] === __dirname + require(__dirname + '/package.json').name)?)

The other one is RANDOM which i guess is covered functionally by Math.random(). I was thinking that in terms of transpiling mapping references to RANDOM to something like Math.floor(Math.random() * 32768)) would be useful. HOME is usually an environment variable. PWD is usually not an environment variable, i assume it was added to bash for efficiency, since PWD === $(pwd -P). But there are whole discussions regarding pwd as a builtin function in some shells but not others and how to handle the value if it contains white space, etc., etc., etc.

I can't say i've ever used PPID, but it's not easily computable at the shell level except by parsing some process stat output which is system dependent.

Brassrat avatar Dec 04 '18 14:12 Brassrat

@Brassrat can you link to docs for each of these?

nfischer avatar Dec 05 '18 03:12 nfischer

BASH_SOURCE, PWD, RANDOM, PPID, HOME are documented in bash man page.

BASH_SOURCE An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}.

I think only BASH_SOURCE[0] make sense for shelljs-transpiler.

PPID The process ID of the shell's parent. This variable is readonly. PWD The current working directory as set by the cd command. RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset. Also, http://tldp.org/LDP/abs/html/randomvar.html

if you google 'bash pwd' you get pages like [https://unix.stackexchange.com/questions/173916/is-it-better-to-use-pwd-or-pwd] which discuss use of $(pwd) vs $PWD.

Brassrat avatar Dec 05 '18 15:12 Brassrat