0

I am building an OS for a virtual machine I made, and while progress has been coming along swimmingly, I have been stuck for a good several hours on what should be a very simple issue: I am designing a method of directory navigation and while I can drop down to a child directory, I am having some issue returning to its parent directory. I need the bash script to allow returning to the parent directory only if the number of characters in the directory is of at least a certain length, as to not allow navigating out above the VM's filesystem into the host FS or system directories of the VM when in user mode, but to allow both when in root mode (to manage which there is a second script that is working fine.).

So here is what I have tried so far, the issue being that both scripts ended up preforming the same action: only returning that "Filesystem root has no parent directory" even when navigating up one dir would be returning to the user mode root rather than going up into the regular root dirs.

#!/bin/bash
dir=$(cat '/system/framework/usrDirectory')
parentdir="$(dirname "$dir")"
echo $parentdir > '/system/framework/parentToCheck'
validCheck=$(cat "/system/framework/parentToCheck")
valid=${#vaidCheck}
validLength=46
if [[ $valid == $validLength ]]
  then
      echo $parentdir > '/system/framework/usrDirectory'
  else if [[ $valid < $validLength ]]
      then
          echo -e "\n\nFilesystem root does not have a parent directory!"
  fi
fi
As well as:
#!/bin/bash
dir=$(cat '/system/framework/usrDirectory')
parentdir="$(dirname "$dir")"
echo $parentdir > '/system/framework/parentToCheck'
validCheck=$(cat "/system/framework/parentToCheck")
valid=${#vaidCheck}
validLength=46
if [[ $valid == $validLength ]]
  then
      echo -e "\n\nFilesystem root does not have a parent directory!"
  else
      echo $parentdir > '/system/framework/usrDirectory'
fi

NOTE: Yes, it does say that the validLength variable's value is 46, and the directory is not that many characters long in this script, it is because this is a privatized VM and user mode is meant to securely lock down access to everything in its parent directories, period. Thus I did not want to display the full path to the user mode root directory here. Nevertheless the path to the user mode's root directory does contain 46 characters, so I can think of no reason this should not work.

closed as off-topic by Stephen Harris, Anthon, Braiam, sam, slm Jul 30 '16 at 21:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Stephen Harris, Anthon, Braiam, sam, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You have a typo in both scripts; valid=${#vaidCheck} - That says vaidCheck; should be validCheck – Stephen Harris Jul 22 '16 at 21:54
  • Looks like you are trying to create a chroot jail, or container. There are some good tools to allow you to do this. Using the low-level tools is tricky and unnecessary; have a look at a high-level tool like docker. I have not yet used docker, but could be what you need. I have used chroot jail tools, and they work well, but do some research into the security issues (for chroot and containers). Ether will work better that anything we can do ourselves. – ctrl-alt-delor Jul 22 '16 at 23:14
3

To answer the question posed in the title succinctly:

if [[ "$foo" -lt "$bar" ]]; then
    echo "$foo is less than $bar"
fi

The tests for integer comparisons are:

 n1 -eq n2     True if the integers n1 and n2 are algebraically equal.

 n1 -ne n2     True if the integers n1 and n2 are not algebraically equal.

 n1 -gt n2     True if the integer n1 is algebraically greater than the
               integer n2.

 n1 -ge n2     True if the integer n1 is algebraically greater than or
               equal to the integer n2.

 n1 -lt n2     True if the integer n1 is algebraically less than the inte-
               ger n2.

 n1 -le n2     True if the integer n1 is algebraically less than or equal
               to the integer n2.

Using e. g. < or > does an ASCII order comparison, so [[ 0100 < 100 ]] will be true.

  • Stephen Harris was right that I had placed a typo, and you were right that I had used incorrect syntax. However even after correcting both issues neither script work yet still. /: – Alison E.E. Jul 23 '16 at 0:13
  • Add set -x toward the top of your script and run it with that in place. That will show you what the script is actually doing, so that you can compare that to what you think it's doing and edit accordingly. – DopeGhoti Jul 23 '16 at 1:16
0

Well, I corrected the typo, which was certainly breaking the programme, and I fixed the syntax of the operator, also definitely breaking it. However (and this may seem ridiculous), once I corrected those issues, I preformed the valid=${#validCheck} operation manually to test the length of the string just in case and found that the directory's length was actually 45 characters, not 46. I feel like a dope right now, but I'm glad the issue has been solved that I may move on to more pressing functions. Thank you both for your corrections to my typos and thank you richard for your suggestion of chroot jail tools.

Not the answer you're looking for? Browse other questions tagged or ask your own question.