Hot answers tagged

9

Several ways; the two most obvious are: Put $1 in double quotes: if [ "$1" = "--test" ] Check for number of arguments using $# Better yet, use getopts.


8

The script below should do the job: #!/usr/bin/env python3 import random import os import subprocess import shutil # set the desired number of folders to pick below n_selection = 5 # set the name of the flash drive below flashdr = "Lexar" # set the source directory (with media folders) below sourcedr = "/path/to/mediafiles" # --- try: targetdr = ...


6

You need to quote your variables inside the if condition. Replace: if [ $1 = "--test" ] || [ $1 = "-t" ]; then with: if [ "$1" = '--test' ] || [ "$1" = '-t' ]; then Then it will work: ➜ ~ ./test.sh Not testing. Always always double quote your variables!


6

you could use find and shuf: #!/bin/bash SOURCE="path/to/source" DESTINATION="path/to/destination" COUNT=25 rm -r "${DESTINATION}/"* find "$SOURCE" -mindepth 2 -maxdepth 2 -type d|shuf -n $COUNT|xargs -d'\n' -I{} cp -r "{}" "$DESTINATION"


5

To test if arguments are present (in general, and do the actions accordingly) This should work: #!/bin/bash check=$1 if [ -z "$check" ]; then echo "no arguments" else echo "there was an argument" fi


2

Background script to check for new screens Once per 5 seconds, the background script below looks for new screens to be connected. If that happens, it tries to set the resolution to what you set in the head section of the script (see: How to use). A notification shows if the creen was successfully set: If the command to set the resolution fails, it ...


2

Fortunately, Dropbox recently expanded their CLI capabilities to include sharelinks etc. To integrate Dropbox into Thunar do the following: Initial Setup Create a ~/bin directory in your home folder if not already created. Download the Dropbox python script. Rename it as just dropbox. (The extension is not needed.) Make it executable chmod +x ...


2

Running an executable file that exists in the same directory you are in, requires the use of ./ in the beginning of the filename. In this case, the correct sytax to use in the terminal is: ./convert-py3k + Enter There should be NO spaces between ./ and the filename.


1

apt-get autoremove, removes files that have been installed automatically and are no longer needed. If you want autoremove not to remove some of them, you can mark them as installed manually. In your case: sudo apt-mark manual package_name. If you have installed a mainline kernel, you need to mark 3 packages: linux-headers-version ...


1

It seems to run on startup. It shows me when I run after a reboot: sudo service rng-tools status The following: ● rng-tools.service Loaded: loaded (/etc/init.d/rng-tools; bad; vendor preset: enabled) Active: active (exited) since Do 2016-05-26 20:17:21 CEST; 22min ago Docs: man:systemd-sysv-generator(8) Process: 1599 ...


1

You can use process substitution, so that use would look like: use() { . <(. ./hello_world.sh; declare -f "$@") } Side note: declare is not POSIX, /bin/sh may not support it, so use.sh shouldn't have /bin/sh in the shebang. (Not that it matters much, since use.sh is sourced instead of executed.)


1

One simple way would be to set an array with the four values you want to cycle through, and then use the % (modulo) operator in your for loop to determine which of the four values to assign to arr[$a] For example: #!/bin/bash values=("apple", "banana", "orange", "grape") ##################################### for((a=0;a<=100;a++)) do ...


1

I created a script for one of my monitors and run it as I connect the monitor. So you can make similar different scripts for your different monitor types and run each as per requirement. #!/bin/bash sudo xrandr --addmode VGA-0 1440x900_60.00 sudo xrandr --newmode Modeline "1440x900_60.00" 106.47 1440 1520 1672 1904 900 901 904 932 -HSync +Vsync ...


1

I constructed a couple of desktop shortcuts to achieve this sort of thing on my laptop. They are $ cat LowRes.desktop #!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Type=Application Terminal=false Icon[en_GB]=gnome-panel-launcher Name[en_GB]=LowRes Exec=xrandr -s 8 Comment[en_GB]=1368x768 Name=LowResDisplay Comment=1368x768 Icon=gnome-panel-launcher ...


1

I made a script with Python: The script, at GitHub Gist. (Download) Usage: python3 RandomCopier.py [source folder] [destination folder] [number to copy] The copy method: NOTE: It will not copy any files directly in the source folder, only those in sub-folders of it. Say, the source folder, src is: src |- a | |- file_a | |- file_a_2 | |- b | |- ...


1

This can be done using VirtualBox Command Line Management Interface. Just run the following script at start up. #!/bin/bash VMUSER="vmuser" VMNAME="RedHatVM" #also you can put UUID in this variable (VMNAME="4ec6acc1-a232-566d-a040-6bc4aadc19a6") case "$1" in start) echo "===Starting VirtualBox VM===" sudo -H -u $VMUSER VBoxManage ...



Only top voted, non community-wiki answers of a minimum length are eligible