#] #] ********************* #] "$d_SysMaint"'Linux/1_bash notes.txt' # www.BillHowell.ca ?02Mar2017? initial "$d_SysMaint"'Linux/bash_scripts.txt' # www.BillHowell.ca 07Jan2018 initial notes (now both are together) # view in text editor, using constant-width font (eg courier), tabWidth = 3 see also "$d_SysMaint"'Linux/bash_scripts list of Howells.txt' for setup of bash environmental variables etc, see : ~/.bashrc #48************************************************48 #24************************24 # Table of Contents, generate with : # $ grep "^#]" "$d_SysMaint"'Linux/1_bash notes.txt' | sed "s/^#\]/ /" # ********************* "$d_SysMaint"'Linux/bash notes.txt' - ??? 04Oct2023 search "Linux bash and how do I put multiple statements on same line?" 25Sep2023 search "Linux and how do I count the lines in a file?" 15Sep2023 search "customize linux terminal prompt" 07Aug2023 search "is bash scripting used anymore?" 04Aug2023 search "multiline string bash script" 29Jul2023 can't one-line bash function definitions!! 14Mar2023 search 'Linux and how do I select lines in a file with a regular expression?' 05Mar2023 search 'Linux bash I echo a line feed?' 01Mar2023 search 'Linux bash and how do I assign the lines of a file to an array?' readarray 28Feb2023 search 'Linux bash and how do I get the total number of elements of an array?' 28Feb2023 search 'Linux bash and how do I access elements of an array?' 28Feb2023 search 'Linux and how do I store a full text file in a variable list?' 26Feb2023 eval - bash execution of strings 26Feb2023 search 'Linux and how do I store a full text file in a variable?' 16Feb2023 str_inWebPages_pContainL() in fileops.sh Library 07Jul2022 must use different varNames in bash script functions!!! 23Mar2021 add quarter-window specs for Markets 04Jan2020 search "Linux bash script and how do I stop script execution and wait?" 14Apr2019 search "linux and bash include a shell file" 01May2018 read options 30Apr2018 Problems with sleep function freeze with LibreOffice 28Apr2018 Problems with "/media/bill/SWAPPER/bin/starter/start_ToDos.sh" 05Jan2018 assignments to variables 12Jan2018 if conditionals 10Jan2018 bash script test if a directory exists 10Jan2018 Variable scoping 09Jan2017 list of [bash,linux] commands 09Jan2018 search "" 09Jan2018 search "Linux and bash script keyboard inputs" 13Oct2020 awesome tips 15Jan2018 Cool - work frequency analysis and many other overall text processing utilities 07Jan2018 search "Linux and bash convert strings to numbers" 07Jan2018 search "Linux and bash arguments to a call" 07Jan2018 search "Linux and bash if statement" 07Jan2018 search "Linux and bash assign a value to a function output" #24************************24 # Setup, ToDos, #] +-----+ #] Reminders #] xargs - for one of commands (find?), won't work with echo #] -> use as example (but change null to something?) : #] echo "$line_input" | tr [:blank:] \\n | start_input.sh #] find "/home/bill/" -name "?*.sh" -print0 | xargs -0 echo >>"$p_out" #] to include a bash file (eg header) : source #] can't one-line bash function definitions!! #] multiEchoReturns - all echos in function are cumulative for return #] +-----+ #24************************24 #08********08 #] ??Feb2024 #08********08 #] ??Feb2024 #08********08 #] ??Feb2024 #08********08 #] 24Feb2024 search "Linux and how do I generate a random number?" +-----+ https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash In (()), you can skip the prefix $ of variable, like this $((1 + RANDOM % 10)). – LianSheng Dec 29, 2020 at 17:18 +-----+ https://www.baeldung.com/linux/random-numbers I didn't check this - some other time #08********08 #] 26Dec2023 mouse moves, but click doesn't work >> geany caused it? close last window opened? >> might be due to geany not up to date with freedesktop.org? common primitives for diverse Linux implementations (like LMDE etc) seems that the problem is [hang-up, freeze], must wait 10s or more for it to come back? #08********08 #] 04Oct2023 search "Linux bash and how do I put multiple statements on same line?" Howell, worked for if statements : if [ -f "$pLnkBad901" ]; then rm "$pLnkBad901"; fi +-----+ https://unix.stackexchange.com/questions/47750/combining-multiple-commands-into-a-single-line Combining multiple commands into a single line Asked 11 years ago Modified 3 years, 7 months ago Viewed 70k times +--+ The symbols you are looking for are called control operators. You are using the wrong ones - you need ; or &&. ; commands are executed in order, and it does not matter if the previous one ended with success or not. && every command that fails will prevent the next ones from executing. You can read more for example in 8.1. control operators edited Feb 13, 2020 at 22:30 Paulo Tomé answered Sep 10, 2012 at 21:38 Paweł Rumian +--+ In case you're using bash, may I suggest you read the man page of bash (man bash), and search for "control operator" (/control operator + [Enter]). There, the different functions of things like | and ; are explained. You might want to look into using && instead of ;, though. If you do that, you can perform the following functions in that sequence only if the previous one was successful. Meaning, if find cannot find your file, it wouldn't try to compress nothing: find -name deleteme.tar && tar xvf deleteme.tar && rm -r delete/* || echo "something went wrong" 1>&2 Just a minor suggestion, though... edited Sep 10, 2012 at 21:41 answered Sep 10, 2012 at 21:34 polemon #08********08 #] 25Sep2023 search "Linux and how do I count the lines in a file?" wc again, I forgot +-----+ https://codefather.tech/blog/count-lines-linux/ How to Count the Lines of a File in Linux February 24, 2020 Claudio Sabato wc -l /var/log/messages #08********08 #] 15Sep2023 search "customize linux terminal prompt" +-----+ https://phoenixnap.com/kb/change-bash-prompt-linux How To Customize Bash Prompt in Linux May 12, 2020 Add Date and Time to The BASH Prompt Use the following options to display different formats for date and time: d – Displays today’s date in [weekday]/[month]/[day] export PS1="\u@\H>\d " t – Displays the current time in 24-hour notation export PS1="\u@\H>\t " T – Displays the current time in 12-hour notation export PS1="\u@\H>\T " A – Displays the current time in 24-hour notation, with just hours and minutes export PS1="\u@\H>\A " More BASH Prompt Options Here is a list of most of the options you can use for the BASH prompt. Some of these commands may not work on all versions of Linux. \a – A bell character \d – Date (day/month/date) \D{format} – Use this to call the system to respond with the current time \e – Escape character \h – Hostname (short) \H – Full hostname (domain name) \j – Number of jobs being managed by the shell \l – The basename of the shells terminal device \n – New line \r – Carriage return \s – The name of the shell \t – Time (hour:minute:second) \@ – Time, 12-hour AM/PM \A – Time, 24-hour, without seconds \u – Current username \v – BASH version \V – Extra information about the BASH version \w – Current working directory ($HOME is represented by ~) \W – The basename of the working directory ($HOME is represented by ~) \! – Lists this command’s number in the history \# – This command’s command number \$ – Specifies whether the user is root (#) or otherwise ($) \\– Backslash \[ – Start a sequence of non-displayed characters (useful if you want to add a command or instruction set to the prompt) \] – Close or end a sequence of non-displayed characters >> I tried $ export PS1="\u@\H>\t " bill@lmde>12:19:20 export PS1="\A> " bill@lmde>12:19:20 export PS1="\A> " 12:20> export PS1="\t> " 12:21:57> 12:20> export PS1="\t> " 12:24> export PS1="\A\$ " 12:35$ export PS1="\D{format} \A\$ " export PS1="~\n\A\$ " ~ 08********08 #] 07Aug2023 search "is bash scripting used anymore?" +-----+ https://duckduckgo.com/?q=is+bash+scripting+used+anymore%3F&atb=v355-1&ia=web Is there any use for Bash scripting anymore? [closed] Asked 13 years, 2 months ago Modified 3 years, 1 month ago Viewed 53k times The real difference between bash and python is that python is a general purpose scripting language, while bash is simply a way to run a myriad of small (and often very fast) programs in a series. Python can do this, but it is not optimized for it. The programs (sort, find, uniq, scp) might do very complex tasks very simply, and bash allows these tasks to interoperate very simply with piping, flushing output in and out from files or devices etc. While Python can run the same programs, you will be forced to do bash scripting in the python script to accomplish the same thing, and then you are stuck with both python and bash. Both are fine by them self, but a mix of these don't improve anything IMHO. answered May 20, 2010 at 8:43 daramarak #08********08 #] 04Aug2023 search "multiline string bash script" +-----+ https://stackoverflow.com/questions/7875540/how-to-write-multiple-line-string-using-bash-with-variables #08********08 #] 29Jul2023 can't one-line bash function definitions!! # why dies this generate error? : # /home/bill/web/bin/fileops.sh: line 1850: syntax error: unexpected end of file str_sedEsc() { echo "$1" | sed "$sedEscExpr" } This works str_sedEsc() { echo "$1" | sed "$sedEscExpr" } #08********08 #] 14Mar2023 search 'Linux and how do I select lines in a file with a regular expression?' no much listed except standard stuff Maybe Bill Howell's fileops.sh is the best that there is? see 'fileops.sh' #08********08 #] 05Mar2023 search 'Linux bash I echo a line feed?' +-----+ https://linuxhandbook.com/echo-newline-bash/ Feb 28, 2022 • 2 min read How to echo a New Line in Bash Shell Scripts Team LHB Team LHB echo -e "Name\nAddress\nPhone Number" #08********08 #] 01Mar2023 search 'Linux bash and how do I assign the lines of a file to an array?' readarray +-----+ https://stackoverflow.com/questions/9449417/how-do-i-assign-the-output-of-a-command-into-an-array How do I assign the output of a command into an array? Asked 11 years ago Modified 12 days ago Viewed 117k times +--+ @Charles Duffy linked the Bash anti-pattern docs in a comment, and those give the most correct answer: readarray -t arr < <(grep -n "search term" file.txt | sed 's/:.*//') His comment: Note that array=( $(command) ) is considered an antipattern, and is the topic of BashPitfalls #50. – Charles Duffy Nov 16, 2020 at 14:07 answered Feb 16 at 21:13 Asfand Qazi YES!! readarray -t lineLOld146 < <( cat "$pOld146" ) # | sed '1~2d' In GNU sed, the address n~m addresses every m:th line starting at line n. This is a GNU extension to standard sed, for convenience. num146=${#lineLOld146[@]} echo "numTotal: $num146" for (( i146=0; i146<${num146}; i146++ )); do echo "lineOld[$i146]: ${lineLOld146[$i146]}" done result : $ bash "$d_bin"'fileops run.sh' /home/bill/web/bin/fileops.sh: line 843: $: command not found /home/bill/web/bin/fileops.sh: line 844: No: command not found /home/bill/web/Bill Howells videos/Birkeland rotation in galaxy - not dark matter/Birkeland rotation in galaxy - not dark matter.html numTotal: 9 lineOld[0]: /home/bill/web/Bill Howells videos/Birkeland rotation in galaxy - not dark matter/Birkeland rotation in galaxy - not dark matter.html lineOld[1]: /home/bill/web/economics, markets/PE Schiller forward vs 10yr Tbills/S&P 500 Shiller-forward PE versus 10y Treasury bond rates.html lineOld[2]: /home/bill/web/page hosted subsites.html lineOld[3]: /home/bill/web/Projects - major/Charvatova solar inertial motion & activity/_Charvatova - solar inertial motion & activity.html lineOld[4]: /home/bill/web/Projects - major/Civilisations and sun/_Civilisations and the sun.html lineOld[5]: /home/bill/web/webOther/Neil Howell/_Neil Howell.html lineOld[6]: /home/bill/web/Projects - major/Solar modeling and forecasting/_Solar modeling & forecasting.html lineOld[7]: /home/bill/web/Projects - mini/Galactic rays and evolution/_Galactic rays and evolution - life, the mind, civilisation, economics, financial markets.html lineOld[8]: /home/bill/web/Projects - mini/Mythology/Cardona - gods are planets, planets are gods (plus Sun).html +-----+ https://unix.stackexchange.com/questions/542611/bash-script-how-to-assign-each-line-of-command-output-to-values-in-an-array BASH script: How to assign each line of command output to values in an array? Asked 3 years, 5 months ago Modified 3 years, 5 months ago Viewed 6k times Using readarray in the bash shell, and GNU sed: readarray -t my_array < <( my_command | sed '1~2d' ) The built-in readarray reads the lines into an array. The lines are read from a process substitution. The sed command in the process substitution will only output every second line read from my_command (and could also be written sed '1!n;d', or as sed -n 'n;p' with standard sed). In GNU sed, the address n~m addresses every m:th line starting at line n. This is a GNU extension to standard sed, for convenience. The my_command command will only ever be called once. Testing: $ readarray -t my_array < <( seq 10 | sed '1~2d' ) $ printf '%s\n' "${my_array[@]}" 2 4 6 8 10 edited Sep 20, 2019 at 12:29 answered Sep 19, 2019 at 12:48 Kusalananda >> not quite, but handy #08********08 #] 28Feb2023 search 'Linux bash and how do I get the total number of elements of an array?' +-----+ https://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/ How To Find BASH Shell Array Length ( number of elements ) Author: Vivek Gite Last updated: July 15, 2022 How do I find out bash shell array length? You can easily find out bash shell array length using following syntax: ${#ArrayName[@]} #08********08 #] 28Feb2023 search 'Linux bash and how do I access elements of an array?' +-----+ https://linuxhandbook.com/bash-arrays/ Oct 29, 2022 • 5 min read Bash Beginner Series #4: Using Arrays in Bash Ahmed Alkabary ... Accessing array elements in bash The first element of an array starts at index 0 and so to access the nth element of the array you use the n -1 index. For example, to print the value of the 2nd element of your files array, you can use the following echo statement: echo ${files[1]} and to print the value of the 3rd element of your files array, you can use: echo ${files[2]} and so on. Also in same response - [add, delete] elements of array #08********08 #] 28Feb2023 search 'Linux and how do I store a full text file in a variable list?' +-----+ https://unix.stackexchange.com/questions/139072/list-files-and-store-it-in-variables list files and store it in variables Asked 8 years, 8 months ago Modified 6 years, 5 months ago Viewed 52k times Your first line: var=$(find script*) is just making var a single string variable with all of script* in it. It's not an array, so we can't index into it with [] like you want. The script* is actually expanded by the shell, so find isn't doing anything there (unless they're directories, which it doesn't look like they are) — it just gets all the filenames as arguments, checks they exist, and prints them straight back out again. The shell actually does all the work.. Instead, we can make an array, and we can use the shell's glob expansion directly to populate it: files=(script*) When we put the initialiser (the right-hand side of the =) in parentheses we're making an array, which holds multiple values separately. script* will expand to every filename starting with script in the current directory. If there are spaces in the filenames they won't cause the words to be split up the way they would be with commands (backticks or $()) inside the array initialiser. At this point we can read in some user input: select SEL in "${files[@]}" do if ! [ "$SEL" ] then echo "Choose one of the available files." continue fi echo "$SEL will start" "./$SEL" break done We write "${files[@]}" to expand our entire array of filenames cleanly to give to select. The user will be offered a choice of files, and then we enter the do...done block. If $SEL is empty, the user chose a non-existent entry, so we print the prompt and continue so they're asked to choose again. Otherwise, we echo the notification that the script will start and run the script. We quote the name "./$SEL" in case the script name has spaces in it, which would otherwise cause the command name to be treated as ./firstword, with the remaining words as arguments. break stops us going back around and asking the user again; if you want to do that, take it out. The case...of you were using doesn't seem to have much effect in the example you give, but if you do have some separate behaviour depending on the script chosen (and it has to be in this script), you can put that inside the do...done block. answered Jun 25, 2014 at 9:23 Michael Homer #08********08 #] 26Feb2023 eval - bash execution of strings +-----+ https://www.linuxshelltips.com/store-linux-command-in-variable/ How to Store a Linux Command as a Variable in Shell Script Abhilash Mhaisne January 22, 2021 One such feature is storing an entire command in a variable. Let’s see what we mean by that and how it can be done. Basically, Linux shells allow you to store any command in a variable, as a string. This is useful for inline complex statements involving multiple commands, redirects, etc. This variable can then be passed to the program ‘eval‘ so that the contents of the variable are executed as a command. For example, to store the output of command ‘cat test.txt | wc -l‘ in a variable called ‘cnt‘, we can have the following statement in the test.sh script file: #!/bin/bash cnt="cat test.txt | wc -l" Now, to execute this command at any point, we pass the variable as an argument to ‘eval‘, as shown below: #!/bin/bash cnt="cat test.txt | wc -l" eval $cnt #08********08 #] 26Feb2023 How to Add Text to the Beginning of File in Linux +-----+ https://www.linuxshelltips.com/add-text-to-beginning-of-file-in-linux/ How to Add Text to the Beginning of File in Linux Abhilash MhaisneFebruary 8, 2021 Add Text to Beginning of File Using echo and cat Commands As you might know, the echo command prints whatever is passed as the argument to the output, whereas the cat command prints the contents of a file to the output. We can thus pass the new text to be added to echo, pass the file to a cat, combine and write the output to the file. This can be done as follows: $ x=`echo "New Text"; cat filename` $ echo "$x" > filename OR Run the following to insert text on a new line at the beginning. $ sed -i '1i New Text' filename The -i argument tells sed to modify the file itself. Without it, sed automatically creates a copy of the file and performs all modifications on the copy. The string ‘1i New Text’ means: ‘insert’ the string ‘New Text’ on the ‘1st’ line. $ cat samplefile.txt $ sed -i '1i Introduction' samplefile.txt $ $ cat samplefile.txt #08********08 #] 26Feb2023 search 'Linux and how do I store a full text file in a variable?' +-----+ https://stackoverflow.com/questions/9146305/save-a-text-file-in-a-variable-in-bash save a text file in a variable in bash Asked 11 years ago Modified 5 years, 2 months ago Viewed 29k times +--+ The issue is that you have an extra space. Assignment requires zero spaces between the = operator. However, with bash you can use: TEXT=$(> Howell: cool, will grep, sed work? #08********08 #] 16Feb2023 str_inWebPages_pContainL() in fileops.sh Library $ bash "$d_bin"'website urls.sh' find: warning: you have specified the global option -maxdepth after the argument -type, but global options are not positional, i.e., -maxdepth affects tests specified before it as well as those specified after it. Please specify global options before other arguments. Test $ find '/home/bill/web/webWork files/' -type f -name "*.html" | grep --invert-match "z_Old" | grep --invert-match "z_Archive" | sort | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "$1" "FILE" | sed 's|/home/bill/web/webWork files/||' | sed 's|:.*:.*||' | sort -u >"$d_temp"'fLst_webPages temp.txt' olde code -type f -o -name "*.html" | 08********08 #] 07Jul2022 must use different varNames in bash script functions!!! ee "$d_bin""fileops.sh" # NOTE!!! : must use different varNames in dinn_finn_strP_replace() versus dinn_finn_archive_pStrPL_replace() 08********08 #] 23Mar2021 add quarter-window specs for Markets edit bash "$d_bin""starter/start_app.sh" win_leftUpp(), win_leftDwn() "browLU" "browLD" ********* #] 04Jan2020 search "Linux bash script and how do I stop script execution and wait?" https://unix.stackexchange.com/questions/305039/pausing-a-bash-script-until-previous-commands-are-finished#305054 You'll want to use the wait command to do this for you. You can either capture all of the children process IDs and wait for them specifically, or if they are the only background processes your script is creating, you can just call wait without an argument. For example: #!/bin/bash # run two processes in the background and wait for them to finish nohup sleep 3 & nohup sleep 10 & echo "This will wait until both are done" date wait date echo "Done" answered Aug 22 '16 at 15:17 ParanoidGeek search "Linux gpg and why doesn't it wait for a password?" https://unix.stackexchange.com/questions/60213/gpg-asks-for-password-even-with-passphrase#68726 I am in your exact same boat (it worked on Fedora but not Ubuntu). Here is an apparent work around I discovered: echo your_password | gpg --batch --yes --passphrase-fd 0 your_file.gpg Explanation: Passing 0 causes --passphrase-fd to read from STDIN rather than from a file. So, piping the passphrase will get --passphrase-fd to accept your specified password string. edited Oct 8 '18 at 10:14 JigglyNaga answered Mar 21 '13 at 19:19 jonS90 16 adding --batch --yes to the above worked for me. – Ryan Tuck Oct 29 '15 at 15:37 1 But then I get a problem, if I want to encrypt data that is piped into gpg, e.g. echo "encrypt me" | gpg --passphrarse "mypw" -c -o test.gpg. How do I solve this? – con-f-use Mar 16 '17 at 14:36 2 Well, with the Ubuntu version of gpg, echo "encrypt me" | gpg --passphrase "mypassphrase" --batch --quiet --yes --no-use-agent -c -o encrypted.gpg seems to work. – con-f-use Mar 19 '17 at 7:25 2 I'm getting Inappropriate ioctl for device with and without --batch here (on gpg (GnuPG) 2.1.18). – Nico Schlömer Dec 17 '17 at 14:35 2 @RyanGriggs I don’t think so. echo "hello" | cat and echo "hello"| cat both yield the same string. – Torsten Bronger Feb 23 '19 at 15:04 https://unix.stackexchange.com/questions/395875/gpg-does-not-ask-for-password I encrypted one file with gpg -c and closed the terminal. After a while, I tried to decrypt it with gpg and it decrypted it, without asking for a password. Is that normal? How to guarantee that gpg will ask for a password, even in my same computer? gpg shareimprove this question asked Oct 3 '17 at 15:31 Pierre B 1,29322 gold badges1212 silver badges3030 bronze badges How long was it since you last entered you GnuPG password at that point? The gpg-agent caches it for 10 minutes by default (GnuPG 2.2). – Kusalananda♦ Oct 3 '17 at 15:37 Was gpg-agent running? If so it would have cached the credentials (just tested this on my Mac with gpg-agent in memory). – Petro Oct 3 '17 at 15:49 add a comment 5 Answers active oldest votes 12 This is normal, gpg now uses gpg-agent to manage private keys, and the agent caches keys for a certain amount of time (up to two hours by default, with a ten minute inactivity timeout). To change the defaults, create or edit a file named ~/.gnupg/gpg-agent.conf, and use the following entries: default-cache-ttl specifies the amount of time a cache entry is kept after its last use, in seconds (600 by default); max-cache-ttl specifies the maximum amount of time a cache entry is kept, in seconds (7200 by default). After changing these, you’ll need to reload the configuration (try sending SIGHUP to gpg-agent, or killing it outright). shareimprove this answer answered Oct 3 '17 at 15:37 Stephen Kitt ***************** #] 14Apr2019 search "linux and bash include a shell file" https://stackoverflow.com/questions/10823635/how-to-include-file-in-a-bash-shell-script#10823650 Simply put inside your script : source FILE Or . FILE it's the same thing. $ LANG=C help source source: source filename [arguments] Execute commands from a file in the current shell. Read and execute commands from FILENAME in the current shell. The entries in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed. Exit Status: Returns the status of the last command executed in FILENAME; fails if FILENAME cannot be read. answered May 30 '12 at 20:20 Gilles Quenot **************************** #] 01May2018 read options NOTE : I still need to find a solution for keyboard inputs in nested scripts!! (...later) # http://wiki.bash-hackers.org/commands/builtin/read Option Description -a read the data word-wise into the specified array instead of normal variables -d recognize as data-end, rather than -e on interactive shells: use Bash's readline interface to read the data -i preloads the input buffer with text from , only works when Readline (-e) is used -n reads characters of input, then quits -N reads characters of input, ignoring any delimiter, then quits -p the prompt string is output (without a trailing automatic newline) before the read is performed -r raw input - disables interpretion of backslash escapes and line-continuation in the read data -s secure input - don't echo input if on a terminal (passwords!) -t wait for data seconds, then quit (exit code 1). Fractional seconds ("5.33") are allowed since Bash 4. A value of 0 immediately returns and indicates if data is waiting in the exit code. Timeout is indicated by an exit code greater than 128. If timeout arrives before data is read completely (before end-of-line), the partial data is saved. -u use the filedescriptor number rather than stdin (0) When both, -a and a variable name is given, then the array is set, but not the variable. Of course it's valid to set individual array elements without using -a: read MYARRAY[5] https://stackoverflow.com/questions/11704353/bash-nested-interactive-read-within-a-loop-thats-also-using-read You've correctly identified that the cause is that within the while ...; do ...; done <<< "$shows" loop, stdin has been redirected, thus read is no longer reading from the keyboard. You can solve this by using a file descriptor other than 0; for example, while read -r -u 3 line; do ...; done 3<${HOME}/.get_iplayer/tv.cache will use FD 3 for the file rather than FD 0, allowing the normal read (without -u) to use original stdin, or while ...; do read -n 1 -p "do stuff? [y/n] : " -u 3 resp; done 3<&0 <<< "$shows" to clone the original FD 0 to FD 3 before replacing FD 0 with your string. answered Jul 28 '12 at 21:08 ephemient >> I'll try : read -n 1 -p "do stuff? [y/n] : " -u 3 resp; done 3<&0 <<< "$shows" https://unix.stackexchange.com/questions/213110/exiting-a-shell-script-with-nested-loops Interesting capture of exit/break conditions Useful for future but not here ***************************** #] 30Apr2018 Problems with sleep function freeze with LibreOffice see "/media/bill/SWAPPER/bin/starter/0_start notes.txt" ************************** #] 28Apr2018 Problems with "/media/bill/SWAPPER/bin/starter/start_ToDos.sh" It freezes while loading the first spreadsheet Check "/media/bill/SWAPPER/bin/starter/start_MindCode.sh" >> OK Check "/media/bill/SWAPPER/bin/starter/start_Suspicious.sh" >> OK Check "/media/bill/SWAPPER/bin/starter/start_Lucas.sh" >> Again - ToDos hung up the LibreOffice start, then froze on "Howell - Background math, summary listing of Chapter 4 formulae.odt" >> LibreOffice - what gives? >> open manually "Howell - Background math, summary listing of Chapter 4 formulae.odt" >> OK Again, Check "/media/bill/SWAPPER/bin/starter/start_Lucas.sh" >> Now stuck after successfully opening "Howell - Background math, summary listing of Chapter 4 formulae.odt" Problem seems to be with LibreOffice, perhaps something corrupted when transferred [PROJECTS,SWAPPER] from RaspPi to LMDE2? I couldn't find a setting in LibreOffice (duh!) and didn't look on the blogs. Instead, I loaded 3 or 4 docs & spreadsheets then tried "/media/bill/SWAPPER/bin/starter/start_Lucas_symbols.sh" >> OK, includingo LibreOffice in load!! Again, Check "/media/bill/SWAPPER/bin/starter/start_ToDos.sh" >> OK, now it works like a charm ********************** #] 05Jan2018 assignments to variables see "/media/bill/SWAPPER/bin/starter/start_app.sh" width_HDMI_half="$(($width_HDMI/2-5))" see "/media/bill/SWAPPER/bin/start.sh" start_basic_all_flag=$( echo "$line_input" | grep --only-match "9" ) echo "start_basic_all_flag= $start_basic_all_flag" if [ "$start_basic_all_flag" = "9" ]; then echo "$start_basic_all" | tr '[:blank:]' '\n' | xargs --max-args=1 bash "$d_start""start_input.sh" line_input=$(( echo "$line_input" | grep --invert-match "9" )) fi if [ "$line_input" != "q" ]; then echo "$line_input" | tr '[:blank:]' '\n' | xargs --max-args=1 bash "$d_start""start_input.sh" fi ********************** #] 12Jan2018 if conditionals see "/media/bill/HOWELL_BASE/System_maintenance/Linux/bash_scripts.txt" # search "Linux and if conditional for multiple string compares" # https://stackoverflow.com/questions/21157435/bash-string-compare-to-multiple-correct-values #Instead of saying: #if [ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]; then #say: #if [[ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]]; then #You might also want to refer to Conditional Constructs. #answered Jan 16 '14 at 9:07, devnull if [[ "$HOWELL_computer" = "Lenovo" || "$HOWELL_computer" = "Dell64" ]]; then filemgr="nemo" terminal="gnome-terminal" browser="firefox" editor="kwrite" libroff="libreoffice" pdf="xreader" elif [ "$HOWELL_computer" = "RaspPi" ]; then filemgr="pcmanfm" terminal="lxterminal" browser="epiphany" browsertab="epiphany" editor="leafpad" libroff="libreoffice" pdf="evince" fi ******************* #] 10Jan2018 bash script test if a directory exists https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script To check if a directory exists in a shell script you can use the following: if [ -d "$DIRECTORY" ]; then # Control will enter here if $DIRECTORY exists. fi Or to check if a directory doesn't exist: if [ ! -d "$DIRECTORY" ]; then # Control will enter here if $DIRECTORY doesn't exist. fi However, as Jon Ericson points out (thanks Jon), subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check. E.g. running this: ln -s "$ACTUAL_DIR" "$SYMLINK" if [ -d "$SYMLINK" ]; then rmdir "$SYMLINK" fi Will produce the error message: rmdir: failed to remove `symlink': Not a directory So symbolic links may have to be treated differently, if subsequent commands expect directories: if [ -d "$LINK_OR_DIR" ]; then if [ -L "$LINK_OR_DIR" ]; then # It is a symlink! # Symbolic link specific commands go here. rm "$LINK_OR_DIR" else # It's a directory! # Directory command goes here. rmdir "$LINK_OR_DIR" fi fi Take particular note of the double-quotes used to wrap the variables, the reason for this is explained by 8jean in another answer. If the variables contain spaces or other unusual characters it will probably cause the script to fail. edited May 23 '17 at 12:34, Community answered Sep 12 '08 at 20:07, Grundlefleck ******************* #] 10Jan2018 Variable scoping +-----+ https://unix.stackexchange.com/questions/27555/what-scopes-can-shell-variables-have There is a hierarchy of variable scopes, as expected. Environment The outermost scope is the environment. This is the only scope managed by the operating system and is therefore guaranteed to exist for every process. When a process is started it receives a copy of its parent's environment after which the two become independent: modifying the child's environment does not change the parent's, and modifying the parent's environment does not change that of an already existing child. Shell variables Shells have their own notion of variables. This is where things start to become a bit confusing. When you assign a value to a variable in a shell, and that variable already exists in the environment, the environment variable receives the new value. However if the variable is not in the environment yet it becomes a shell variable. Shell variables only exist within the shell process, similar to how Ruby variables only exist within a Ruby script. They are never inherited by child processes. Here's where the export keyword comes into play. It copies a shell variable into the shell process's environment making it possible for child processes to inherit. Local variables Local variables are shell variables scoped to the code blocks containing them. You declare local variables with the typeset keyword (portable) or local or declare (Bash). Like other shell variables local variables are not inherited by child processes. Also local variables cannot be exported. answered Aug 12 '16 at 5:18, SnakE +-----+ ************************* #] 09Jan2017 list of [bash,linux] commands +-----+ shell commands (eg bash) http://www.mediacollege.com/linux/command/shell-command.html learn + help with bash command : great tutorial!! https://www.computerhope.com/unix/ubash.htm +-----+ linux commands http://www.mediacollege.com/linux/command/linux-command.html https://unix.stackexchange.com/questions/94775/list-all-commands-that-a-shell-knows You can usecompgen compgen -c # will list all the commands you could run. FYI: compgen -a # will list all the aliases you could run. compgen -b # will list all the built-ins you could run. <- ***** normally what I want compgen -k # will list all the keywords you could run. compgen -A function # will list all the functions you could run. compgen -A function -abck # will list all the above in one go. edited Jul 28 '15 at 11:39, jimmij answered Oct 12 '13 at 17:57, Rahul Patil >> example with grep : compgen -b | grep -e +-----+ Reminders xargs - for one of commands (find?), won't work with echo -> use as example (but change null to something?) : echo "$line_input" | tr [:blank:] \\n | start_input.sh https://unix.stackexchange.com/questions/27555/what-scopes-can-shell-variables-have At least under ksh and bash, variables can have three scopes, not two like all remaining answers are currently telling. In addition to the exported (i.e. environment) variable and shell unexported variable scopes, there is also a third narrower one for function local variables. Variables declared in shell functions with the typeset token are only visible inside the functions they are declared in and in (sub) functions called from there. edited Dec 24 '11 at 12:11 asked Dec 24 '11 at 3:07, Nathan Long ******************* #] 09Jan2018 search "" https://stackoverflow.com/questions/1464253/global-environment-variables-in-a-shell-script http://www.softpanorama.org/Tools/tr.shtml great description & examples of tr ******************* #] 09Jan2018 search "Linux and bash script keyboard inputs" https://www.linuxquestions.org/questions/blog/nominal-animal-554844/bash-scripts-and-keyboard-input-34422/ nice example (too long to paste here), but not what I need search "Linux and bash script keyboard input list" https://stackoverflow.com/questions/32864392/how-to-pass-multiple-keyboard-inputs-along-with-linux-command >> nyet search "Linux and bash script keyboard input" https://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard read -p "Prompt" variable1 variable2 variableN I simply used two scripts >> one got a line of keyboard input, and split it up by spaces for piping to a second script : read -p "Enter a list of numbers, separated by single spaces : " line_input if [ "$line_input" != "q" ]; then echo "$line_input" | tr [:blank:] \\n | start_input.sh fi >> the second script acted on each "command" : if [ "$1"="1" ]; then start_base.sh elif [ "$1"="2" ]; then start_HOWELL_BASE.sh ... etc, etc There is probably a simpler "list processing" approach for that ******************** 02Mar2017 #!/bin/sh # /media/bill/HOWELL_BASE/2017 IJCNN Anchorage/mass emails/sort author list.sh sort -u >"170302 IJCNN author emails sort and culled.txt" "170302 IJCNN author emails.txt" # enddoc ********************************* #] 13Oct2020 awesome tips --- Use of read and pipelining : http://stackoverflow.com/questions/7676045/how-to-use-read-command-in-bash dater=$(date +%y%m%d) for my favourite date format in a variable --- for expanding script variables and output : http://stackoverflow.com/questions/2853803/in-a-shell-script-echo-shell-commands-as-they-are-executed --- Weak (") versus strong (') quoting - nothng in strong quotes gets interpreted by bash, it's safer! http://wiki.bash-hackers.org/syntax/quoting 08********08 #] 15Jan2018 Cool - work frequency analysis and many other overall text processing utilities great summary http://www.tldp.org/LDP/abs/html/textproc.html #!/bin/bash # wf.sh: Crude word frequency analysis on a text file. # This is a more efficient version of the "wf2.sh" script. # Check for input file on command-line. ARGS=1 E_BADARGS=85 E_NOFILE=86 if [ $# -ne "$ARGS" ] # Correct number of arguments passed to script? then echo "Usage: `basename $0` filename" exit $E_BADARGS fi if [ ! -f "$1" ] # Check if file exists. then echo "File \"$1\" does not exist." exit $E_NOFILE fi look The command look works like grep, but does a lookup on a "dictionary," a sorted word list. By default, look searches for a match in /usr/dict/words, but a different dictionary file may be specified. Example 16-20. Checking words in a list for validity #!/bin/bash # lookup: Does a dictionary lookup on each word in a data file. file=words.data # Data file from which to read words to test. echo echo "Testing file $file" echo while [ "$word" != end ] # Last word in data file. do # ^^^ read word # From data file, because of redirection at end of loop. look $word > /dev/null # Don't want to display lines in dictionary file. # Searches for words in the file /usr/share/dict/words #+ (usually a link to linux.words). lookup=$? # Exit status of 'look' command. if [ "$lookup" -eq 0 ] then echo "\"$word\" is valid." else echo "\"$word\" is invalid." fi done <"$file" # Redirects stdin to $file, so "reads" come from there. echo exit 0 # ---------------------------------------------------------------- # Code below line will not execute because of "exit" command above. # Stephane Chazelas proposes the following, more concise alternative: while read word && [[ $word != end ]] do if look "$word" > /dev/null then echo "\"$word\" is valid." else echo "\"$word\" is invalid." fi done <"$file" exit 0 +-----+ 15Jan2018 my stab at the cull #do if look -bdf "$line" "$1" > /dev/null # grepper="$( echo "$line" | tr \\n \\0 | xargs -0 grep -e --ignore-case "$badder" )" # if [[ "$badder" = "$liner" ]] # then echo "\"$line\" is valid." # else echo "\"$line\" is invalid." # fi # tester=$( echo "$line" | grep "$badder" ) # fart="$( echo "george" | grep "eo" )" # -e | tr \\n \\0 echo "line = $line" echo "badder = $badder" echo "grepper = $grepper" ######################################################## # main () sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\ /g' "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr # ========================= # Frequency of occurrence # Filter out periods and commas, and #+ change space between words to linefeed, #+ then shift characters to lowercase, and #+ finally prefix occurrence count and sort numerically. # Arun Giridhar suggests modifying the above to: # . . . | sort | uniq -c | sort +1 [-f] | sort +0 -nr # This adds a secondary sort key, so instances of #+ equal occurrence are sorted alphabetically. # As he explains it: # "This is effectively a radix sort, first on the #+ least significant column #+ (word or string, optionally case-insensitive) #+ and last on the most significant column (frequency)." # # As Frank Wang explains, the above is equivalent to #+ . . . | sort | uniq -c | sort +0 -nr #+ and the following also works: #+ . . . | sort | uniq -c | sort -k1nr -k ######################################################## exit 0 # Exercises: # --------- # 1) Add 'sed' commands to filter out other punctuation, #+ such as semicolons. # 2) Modify the script to also filter out multiple spaces and #+ other whitespace. 08********08 #] 07Jan2018 search "Linux and bash convert strings to numbers" strings == comparator integers -eq -lt -gt a=expr'$a/1' 08********08 #] 07Jan2018 search "Linux and bash arguments to a call" https://stackoverflow.com/questions/16988427/calling-one-bash-script-from-another-script-passing-it-arguments-with-quotes-and >> good for one of my problems, but I didn't paste here 08********08 #] 07Jan2018 search "Linux and bash if statement" https://linuxacademy.com/blog/linux/conditions-in-bash-scripting-if-statements/ November 25, 2012 anthony Linux The syntax of an if statement (a short explanation) The basic syntax of an if … then statement is like this: if ; then fi The condition is, depending on its type, surrounded by certain brackets, eg. [ ]. You can read about the different types further on in the tutorial. You can add commands to be executed when the condition is false using the else keyword, and use the elif (elseif) keyword to execute commands on another condition if the primary condition is false. The else keyword always comes last. Example: if [ -r somefile ]; then content=$(cat somefile) elif [ -f somefile ]; then echo “The file ‘somefile’ exists but is not readable to the script.” else echo “The file ‘somefile’ does not exist.” fi >> Many good details on if statements (too large to paste here...) MAJOR PROBLEM!!! Do NOT use "==" in if conditions for strings, use a single "=" if [ "$2" == "pcmanfm" ]; then -> this does NOT work!! if [ "$2" = "pcmanfm" ]; then -> this works 08********08 #] 07Jan2018 search "Linux and bash assign a value to a function output" +-----+ https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-from-a-command-in-bash I am working on a simple scripting project for work that involves the use of Bash. I have a pretty simple script that is something like the following: #!/bin/bash VAR1="$1" VAR2="$2" MOREF='sudo run command against $VAR1 | grep name | cut -c7-' echo $MOREF When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF variable, I am able to get output. I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen? edited Feb 16 '17 at 6:04, codeforester asked Jan 10 '11 at 20:58, John +--+ In addition to the backticks, you can use $(), which I find easier to read, and allows for nesting. OUTPUT="$(ls -1)" echo "${OUTPUT}" Quoting (") does matter to preserve multi-line values. edited Oct 17 '14 at 13:50, user2350426 answered Jan 10 '11 at 21:04, Andy Lester I know three ways to do: 1) Functions are suitable for such tasks: func (){ ls -l } Invoke it by saying func 2) Also another suitable solution could be eval: var="ls -l" eval $var 3) The third one is using variables directly: var=$(ls -l) OR var=`ls -l` you can get output of third solution in good way: echo "$var" and also in nasty way: echo $var edited May 17 '14 at 16:22, BoltClock answered Feb 13 '14 at 7:31, MLSC +--+ Some bash tricks I use to set variables from commands First simple old and compatible way myPi=`echo '4*a(1)' | bc -l` echo $myPi 3.14159265358979323844 Mostly compatible, second way As nesting could become heavy, parenthesis was implemented for this myPi=$(bc -l <<<'4*a(1)') Nested sample: SysStarted=$(date -d "$(ps ho lstart 1)" +%s) echo $SysStarted 1480656334 reading more than one variable (with bashisms) df -k / Filesystem 1K-blocks Used Available Use% Mounted on /dev/dm-0 999320 529020 401488 57% / If I just want Used value: array=($(df -k /)) you could see array variable: declare -p array declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [ 4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]= "401488" [11]="57%" [12]="/")' Then: echo ${array[9]} 529020 But I prefer this: { read foo ; read filesystem size used avail prct mountpoint ; } < <(df -k /) echo $used 529020 1st read foo will just skip header line (variable $foo will contain something like Filesystem 1K-blocks Used Available Use% Mounted on) About sudo cmd | grep ... | cut ... shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7) echo $shell /bin/bash (Please avoid useless cat! So this is just 1 fork less: shell=$(grep $USER