-2

Pseudocode where incomplete pseudo try-catch after cas' comment

#!/usr/bin/env bash
# http://askubuntu.com/q/799834/25388    
# Use: `sh myScript`; sudo does not work with Matlab. 

#set -e # no rudimentary error checking
# https://stackoverflow.com/a/69808/54964

SWAP_FILE="/media/masi/SamiSwapVirtual/.swap_file_20.7.2016"
SIZE_MB="1000"
TO_RUN="matlab"

sudo dd if="/dev/zero" of="${SWAP_FILE}" bs="1M" count="${SIZE_MB}" status="progress"
sudo mkswap "${SWAP_FILE}"
sudo chmod 0600 "${SWAP_FILE}"
sudo chown root:root "${SWAP_FILE}" # http://unix.stackexchange.com/a/297153/16920

# TODO error-handling if any error, do swap
# Error-trapping here as here https://stackoverflow.com/a/185900/54964 ?   
# 
# My proposal where I am not sure about how to connect the commands inside the first block in braces
# 
# https://stackoverflow.com/a/22010339/54964
{
        sudo swapon -v "${SWAP_FILE}"
        echo "Swap enabled. Press enter to continue"; read
        "${TO_RUN}"
} || {
        echo "I will remove the swap. Press enter to continue"; read
        sudo swapoff -v "${SWAP_FILE}"
        rm -vf "${SWAP_FILE}"
        exit 1
}

echo "I will remove the swap. Press enter to continue"; read
sudo swapoff -v "${SWAP_FILE}"
rm -vf "${SWAP_FILE}"

Output

dd: failed to open '/media/masi/SamiSwapVirtual/.swap_file_20.7.2016': Permission denied
mkswap: cannot open /media/masi/SamiSwapVirtual/.swap_file_20.7.2016: Permission denied
chmod: changing permissions of '/media/masi/SamiSwapVirtual/.swap_file_20.7.2016': Operation not permitted
[sudo] password for masi: 

Open

  • dd and mkswap gives Permission denied sometimes depending on the disk so sudo there.
  • How can you put it ask your password first before running any commands in the first block of braces?

System: Linux Ubuntu 16.04 64 bit Linux kernel: 4.6
Hardware: Macbook Air 2013-mid
Related thread: Error handling in BASH

  • 1
  • quote your variables when you use them, not just when you define them. 2. pretty much all of this has to be done as root, so just write the script without any sudo commands in it and run it as sudo myswapscript. 3. just add more swap space to the system in /etc/fstab and be done with it - do you really need to add more swap every time you run matlab? 4. even better than swap, add more RAM to the system. or RAM and swap. 5. the linux kernel's zram (compressed ram block device) makes a nice swap device, and much faster than any hdd or ssd.
  • – cas Jul 21 '16 at 07:18
  • @cas 1) fixed. 2) does not work because Matlab does not work with sudo. – Léo Léopold Hertz 준영 Jul 21 '16 at 14:20
  • 1
    So don't run matlab with sudo. Running matlab is, or should be, entirely separate from managing swap space - which is the theme of my points 3, 4, and 5. The script that runs matlab should not be the same script that manages your swap space. If you really must do it in this crazy way, then your script that runs matlab should use sudo to run the script that adds swap space - they should be separate scripts, because they do completely different and unrelated tasks. – cas Jul 21 '16 at 14:37