Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).
Shell scripting is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.
I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation
VARIABLE1="${VARIABLE1:-some_val}"
or some expand other variables
VARIABLE2="${VARIABLE2:-`echo…
I notice that some scripts which I have acquired from others have the shebang #!/path/to/NAME while others (using the same tool, NAME) have the shebang #!/usr/bin/env NAME.
Both seem to work properly. In tutorials (on Python, for example), there…
I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or string into the script. How do I approach doing…
This question is a sequel of sorts to my earlier question. The users on this site kindly helped me determine how to write a bash for loop that iterates over string values. For example, suppose that a loop control variable fname iterates over the…
It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing.
But people shouldn't need 10 years for this. If I were just starting out with Linux, I'd want to know: When to…
I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an example usage of the 'sed' command.
Say I wanted to…
Take the following script:
#!/bin/sh
sed 's/(127\.0\.1\.1)\s/\1/' [some file]
If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between…
I would like to change a file extension from *.txt to *.text. I tried using the basename command, but I'm having trouble on changing more than one file.
Here's my code:
files=`ls -1 *.txt`
for x in $files
do
mv $x "`basename $files…
Or, an introductory guide to robust filename handling and other string passing in shell scripts.
I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).
I encountered a problem such as the…
I need to find my external IP address from a shell script. At the moment I use this function:
myip () {
lwp-request -o text checkip.dyndns.org | awk '{ print $NF }'
}
But it relies on perl-libwww, perl-html-format, and perl-html-tree being…
I would like to remove all leading and trailing spaces and tabs from each line in an output.
Is there a simple tool like trim I could pipe my output into?
Example file:
test space at back
test space at front
TAB at end
TAB at front
sequence…
The following bash syntax verifies if param isn't empty:
[[ ! -z $param ]]
For example:
param=""
[[ ! -z $param ]] && echo "I am not zero"
No output and its fine.
But when param is empty except for one (or more) space characters, then the…
While running a script, I want to create a temporary file in /tmp directory.
After execution of that script, that will be cleaned by that script.
How to do that in shell script?
I have 2 graphics cards on my laptop. One is IGP and another discrete.
I've written a shell script to to turn off the discrete graphics card.
How can I convert it to systemd script to run it at start-up?
I am aware of three methods to delete all entries from a file.
They are
>filename
touch filename1
filename < /dev/null
Of these three I abuse >filename the most as that requires the least number of keystrokes.
However, I would like to know which…