Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shift function does not work #35

Open
Brassrat opened this issue Nov 30, 2018 · 6 comments
Open

shift function does not work #35

Brassrat opened this issue Nov 30, 2018 · 6 comments

Comments

@Brassrat
Copy link

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

@nfischer
Copy link
Owner

nfischer commented Dec 2, 2018

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 $#.

@Brassrat
Copy link
Author

Brassrat commented Dec 3, 2018

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)

@nfischer
Copy link
Owner

nfischer commented Dec 4, 2018

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 😄. 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.

@Brassrat
Copy link
Author

Brassrat commented Dec 4, 2018

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.

@nfischer
Copy link
Owner

nfischer commented Dec 5, 2018

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

@Brassrat
Copy link
Author

Brassrat commented Dec 5, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants