Random number from a range in a Bash Script
shuf -i 2000-65000 -n 1 Enjoy! Edit: The range is inclusive.
shuf -i 2000-65000 -n 1 Enjoy! Edit: The range is inclusive.
By convention, environment variables (PAGER, EDITOR, …) and internal shell variables (SHELL, BASH_VERSION, …) are capitalized. All other variable names should be lower case. Remember that variable names are case-sensitive; this convention avoids accidentally overriding environmental and internal variables. Keeping to this convention, you can rest assured that you don’t need to know every environment … Read more
First in terminal make the script executable by typing the following command: chmod a+x yourscriptname Then, in Finder, right-click your file and select “Open with” and then “Other…”. Here you select the application you want the file to execute into, in this case it would be Terminal. To be able to select terminal you need … Read more
Add this to your crontab (temporarily): * * * * * env > ~/cronenv After it runs, do this: env – `cat ~/cronenv` /bin/sh This assumes that your cron runs /bin/sh, which is the default regardless of the user’s default shell. Footnote: if env contains more advanced config, eg PS1=$(__git_ps1 ” (%s)”)$, it will error … Read more
This is the exit status of the last executed command. For example the command true always returns a status of 0 and false always returns a status of 1: true echo $? # echoes 0 false echo $? # echoes 1 From the manual: (acessible by calling man bash in your shell) ? Expands to … Read more
sort ip_addresses | uniq -c This will print the count first, but other than that it should be exactly what you want.
Use a trap! tempfiles=( ) cleanup() { rm -f “${tempfiles[@]}” } trap cleanup 0 error() { local parent_lineno=”$1″ local message=”$2″ local code=”${3:-1}” if [[ -n “$message” ]] ; then echo “Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}” else echo “Error on or near line ${parent_lineno}; exiting with status ${code}” fi exit … Read more
Avoid PID-files, crons, or anything else that tries to evaluate processes that aren’t their children. There is a very good reason why in UNIX, you can ONLY wait on your children. Any method (ps parsing, pgrep, storing a PID, …) that tries to work around that is flawed and has gaping holes in it. Just … Read more
Don’t forget about spaces: source=”” samples=(“”) if [ $1 = “country” ]; then source=”country” samples=”US Canada Mexico…” else echo “try again” fi
You can do the following: ssh -t xxx.xxx.xxx.xxx “cd /directory_wanted ; bash –login” This way, you will get a login shell right on the directory_wanted. Explanation -t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t … Read more