0
0
I have a shell script that will be a such of a header to be imported by others scripts
Its main goal is forbid the user of "*" and "?" as parameters.
I've already tried:
1) To get access to the script call via ~/.bash_history As the ~/.bash_history is upgraded at the end of the user session. I modified the ~/.bashrc with the following parameters: shopt -s histappend, PROMPT_COMMAND="history -a;$PROMPT_COMMAND" and also tried PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND".
When I set this setting, the system start to upgrade the ~/.bash_history file in memory time. So, theoretically, I'll be able to get the last command used by the user (that's should be the script call) and to search a "*" on it. But as you might know, a shell script must to specify the shell interpreter in its first line like: /bin/bash, thus, it creates a new shell session ever time when the script is called and this new session doesn't have access to the previous session command history: For exemple:
~>cat hist.sh
#!/bin/bash
var=$(tail -n 1 /home/$USER/.bash_history) # get the last command
echo $var
~>./hist.sh
cat hist.sh # that's the output, it shoud be ./hist.sh
I think the ~/.bash_history file is written just when the process of the command is finished, that's why the ./hist.sh wasn't there, but it is just my guess.
2) I also tried to use the history and the fc -l -n -0 commds (this is currently working in AIX environment), but i got the same previous problem in Linux.
3) My last attempt was the folloing script:
#!/bin/bash
for i in $@
do
if echo $i | grep '*' &> /dev/null
then echo "Invalid parameter *" && exit
else echo "Sucessful"
fi
done
It works when I use ./script.sh file* (the "" + words or "" + words), but it doesn't when I use the "*" alone (./script.sh *), because the shell expands the * to all files of the current directory before the if statements.
I've considered to limitate the number of parameters for the script, but as it will be a header useful for many another script, the number of arguments for each call is definitely unkown.
Could someone help with this issue? I'll be very grateful!
3XY problem? What wrong happens to your scripts when users use
*
or?
in their shells? – Kamil Maciorowski – 2019-09-30T12:51:43.9271It works when I use `./script.sh file
* It works only if there is no
filein the directory, otherwise the file expansion will replace the
file` with the names of the matching files and you won't see the asterisk. – xenoid – 2019-09-30T16:15:59.780+1 to Kamil's remark. Removing wilcards can be a major usability issue. – xenoid – 2019-09-30T16:21:43.283
It was a request of the development team... They just said that in AXI, the rule is currently working and they want that same feature in Linux environment because they have already had problems with this kind of parameter before. – Mauro Matsudo – 2019-09-30T16:31:45.763
2If
*
/?
gets to a script, the script has all means to reject it or handle it in whatever way, because it knows it's there. If*
/?
doesn't get to the script it means the shell expanded it. Regardless of what the expansion result is, the user could have typed the expanded command by hand. So what's the point? – Kamil Maciorowski – 2019-09-30T16:46:43.577Exactly what is working in AIX? (If is isn't a misunderstatement.) – Lorinczy Zsigmond – 2019-10-01T11:10:44.250
Lorinczy in AXI when I use the fc -l -n -0 I got the script call, like ./my script.sh file * – Mauro Matsudo – 2019-10-01T15:42:35.813