The variable tag has no wiki summary.
8
votes
4answers
286 views
Using “${a:-b}” for variable assignment in scripts
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 ...
2
votes
2answers
115 views
Assign value to a variable in bash, coming from another commands
Doing this in bash, makes SUFFIX variable holding /dev/fd/[fd],
SUFFIX=<( echo "&*645\[]}#@name" | sed -r 's/[^[:alnum:]]+/_/g' )
What should I do to assign the actual result, of commands ...
4
votes
4answers
151 views
Pass shell variable as a /pattern/ to awk
Having the following in one of my shell functions:
function _process () {
awk -v l="$line" '
BEGIN {p=0}
/'"$1"'/ {p=1}
END{ if(p) print l >> "outfile.txt" }
'
}
, so when called as ...
6
votes
2answers
179 views
What's the most efficient way to grep for two completely separate things and assign the values to separate variables?
CentOS 6.x
I want to take the output from curl, grep for two completely separate strings, and assign their respective values as variables. What is the most efficient way to do this (without writing ...
1
vote
1answer
90 views
How can I use a loop inside a command?
I'm writing a shell script that contains a command that takes multiple directories as space-separated input arguments (like ls). I would like to fill in those arguments from a variable but don't know ...
2
votes
2answers
119 views
Read non-bash variables from a file into bash script
My bash script needs to read the variables from a text file that contains hundreds of variables, most of which conform to standard bash variable sytax (e.g., VAR1=VALUE1). However, a few lines in this ...
1
vote
1answer
21 views
Need to set a variable with “[]”
I have a tcsh script, in which I need to set a variable to this value defining my system type.
type=abc project=def sysname=["Linux"]
I tried various combinations to escape the [ and " but have ...
3
votes
4answers
82 views
How do I output a suggestion for a user to accept and use as input?
What can I do to leave the cursor on the same line of the suggestion in a Bash script?
Bash version = 3.2.51
It's purpose is to suggest a string so that the user should only press Enter.
#!/bin/sh
...
-3
votes
1answer
32 views
why does the dollar sign cause this command to be executed?
test_script.sh:
#!/bin/sh
D1="$(dirname $0)"
echo $D1
D0="(dirname $0)"
echo $D0
What I type on the command line:
$ ./test_script.sh
Output:
.
(dirname ./test_script.sh)
What significance ...
1
vote
2answers
63 views
How do you preserve a space in a literal expression in an array value in bash and how to trim down results from a sort command?
In the context of searching for expressions in log files, I was wondering generally if there was a way to quantify and qualify the contents of the logs in /var/log in some way. In particular, does the ...
1
vote
2answers
48 views
Nature of the positional parameters
I am beginner to shell scripting and wrote this script which sort of confused me a bit...
function func
{
the variables received are
echo $0: $1 and $2
}
echo in the main script
func ball boy
The ...
2
votes
1answer
71 views
BASH_ALIASES not equal to alias
I've just recognized that (in Debian 7, within Bash 4.2.37) the output of BASH_ALIASES isn't equal to that of alias – which, according to gnu.org's Bash Manual, should be the case:
...
7
votes
1answer
341 views
How to store an image file in bash variable?
After I use the following command,
pngString="$(cat example.png)"
echo -n "$pngString" > tmp.png
I can't open the tmp.png as a PNG file. Maybe some information is lost when I use $pngString ...
-1
votes
2answers
275 views
Append variable string to itself [closed]
I have a variable into a do loop. I append its content to a file, but I want to append it to itself so I could use it to do other stuff like sending mail.
I tried variable+=$variable but it didn't ...
1
vote
0answers
120 views
Shell script to backup VMware VM's, exporting each VM to a list then asking user intput
What I'm Trying To Accomplish:
Get list of Virtual Machines ".vmx" file and place in variables
Give the user and choice of which VM to backup
Use "ovftool" to produce an export of the chosen VM to ...
0
votes
1answer
165 views
Test variable if its string or not
I'm writing a script which will have some arguments and so I am using getopts
but i want to solve the problem with one argument.
I use a switch, for example -d, and I want the argument for -d will ...
2
votes
3answers
200 views
Pipe assigns variable
For simplicity I would like to do
echo cart | assign spo;
echo $spo
Output: cart
Does such an appliance exist such as assign?
I am aware of all the ways to do this using substitution.
2
votes
1answer
75 views
Appending to same array in various loops, only last values remain Bash 4
I have something like this:
FILES=()
for i in *.map
do
FILES+=($i)
done
find /var/candy -name "chocolate_[0-9]" | while read snack
do
FILES+=($snack)
done
for file in ...
1
vote
1answer
66 views
creating variable using variable value as part of new variable name
I'm trying to create a new variable using the value of an existing variable as part of the variable name.
filemsg"$word1"=" "
I've also tried
filemsg$word1=" "
filemsg${word1}=" "
on all ...
-3
votes
1answer
53 views
What does readonly mean or do?
What does readonly mean or do in this case? I have never seen or heard of it.
a="testString"; b=a; readonly b; b=25;
Also, would the final value of b at the end be 25?
1
vote
2answers
135 views
Execution of a command in a variable with eval
I am trying to use grep to color some log files . I wrote a script and a part of it looks like this :
#!/bin/bash
com="GREP_COLOR=\"1;36\" egrep --color=always '[^a-zA-Z0-9]' $log |less -R"
...
0
votes
5answers
78 views
How can I modify variables
I have the following variable:
col=1,3,4,
Is there anyway to select the numbers within the variable: (i.e. 1 3 4 and work with them?
Such as choosing the "1", "3", or "4" column from a .txt file?
...
1
vote
1answer
101 views
prevent bash completion from altering underscore variable $_
When using tab completion in bash, the $_ variable is altered:
$ mkdir test
$ cd <TAB><TAB> $_
bash: cd: -d: invalid option
cd: usage: cd [-L|[-P [-e]]] [dir]
(The ...
1
vote
4answers
154 views
Reusing a variable
I was breaking my head for a day still not successful.
I have this script:
#!/bin/ksh
fname=$1
for batchname in $(grep -i "Processing batch" $fname | cut -d "'" -f2)
do
Batch_state=`grep -c -i ...
0
votes
2answers
78 views
Why must you be careful when using Bash's built in command history function to re-run previous commands that contain variables?
I know !! re-runs commands but what exactly would occur if I re-ran a command that had a variable in the command?
0
votes
1answer
158 views
LD_LIBRARY_PATH not found - CentOS
I am trying to run the following two commands...
LD_LIBRARY_PATH =/opt/oracle/instantclient_12_1
export
LD_LIBRARY_PATH
But when I run the first command I get an error
-bash: ...
1
vote
2answers
4k views
how can I add (subtract, etc.) two numbers with bash?
I can read the numbers and operation in with:
echo "First number please"
read num1
echo "Second mumber please"
read num2
echo "Operation?"
read op
but then all my attempts to add the numbers fail:
...
1
vote
2answers
364 views
awk,how to change the value of the file using parameter sets?
I am trying to reset my data using awk.
I have a data of 54 columns where I want to change six of the column values using the following script.
#!/bin/bash -x
# optimal parameters
set ...
2
votes
2answers
172 views
Creating temp file vs process substituion vs variable expansion?
If I am doing something like
creating temporary file
some process generating output > temp_file
cat temp_file
process substitution:
cat <(some process generating output)
another way :
...
2
votes
4answers
236 views
Scope of variables when calling function from find
In a bash script I define a function that is called from find. The problem is that the scope of variables does not extend to the function. How do I access variables from the function? Here is an ...
2
votes
1answer
64 views
How to take advantage of gnuplot internal functions by column reference at shell
I want to do something like:
echo "p 'myfile.txt' u 1:(exp($2))" | gnuplot -p
but need the shell to pass the $2 as a literal into gnuplot.
2
votes
4answers
375 views
Can awk use field identifiers also for shell strings (variables)?
Well, this apparently is not possible the way I'm trying it.
This alternate approach to obtain bar as a resulting string works, though:
#!/bin/bash
path1=/usr/tmp/foo/bar/baz
awk -F/ '{print $5}' ...
2
votes
4answers
323 views
Setting a variable whose value depends on another variable
i have a text file variables.txt where i store my variables
host=/srv/www/vhosts/hosting
index=$host/index.php
as you see the second variables depends on the first one.
i also have a ...
2
votes
1answer
119 views
prevent trap from altering underscore variable $_
I have the following code in my bashrc to get the execution time of the last command from http://stackoverflow.com/a/1862762
function timer_start {
timer=${timer:-$SECONDS}
}
function timer_stop {
...
43
votes
2answers
2k views
What NOT to put on an SSD?
I bought an SSD and I am going to set up my desktop system with a completely fresh Linux installation.
SSDs are known to be fast, but they have a disadvantage: The number of writes (per block?) is ...
1
vote
2answers
52 views
Associating variable with specific for loop file
I want to use for for a set of files.
for file in fileA fileB fileC; do
COMMAND $file $variable
done
For every $file I want to use specific $variable.
(for example: fileA variableA; fileB ...
1
vote
2answers
293 views
for loop to get more than one arguments
In python and some other programming languages, it is easy to get a vector instead of one variable everywhere and in loops. like python:
for variable in ...
0
votes
1answer
76 views
How can I use a variable from a script?
A bash script is running as I defined it in Startup Applications. It is possible to display on terminal a variable used in that script? If yes, how?
2
votes
1answer
128 views
What does “${x%% *}” mean in sh? [duplicate]
I just saw "$${x%% *}" in a makefile, which means "${x%% *}" in sh.
Why it is written in this way ?
how can a makefile detect whether a command is available in the local machine?
determine_sum = \
...
0
votes
2answers
2k views
bash - reading user variable into bash script grep
I've tried every possible combination to get this bash script working. It's part of a larger script, and it basically prompts for a username (to check if it exists) and returns the appropriate ...
2
votes
2answers
84 views
List all my variables [duplicate]
I'm just learning the basics, including how to declare and mess around with variables. Is there a simple way to display a list of all the variables I've named? (Everything I've found by searching only ...
0
votes
1answer
414 views
How can I preserve new lines coming from a command's output during variable assignment?
Consider:
$ getfacl somefile.dat # The output is formatted and contains several new lines..
# file: somefile.dat
# owner: user1
# group: group1
user::rw-
group::r-- #effective:r--
...
1
vote
2answers
139 views
How to print shell variables and values to be able to copy/paste them?
In Bash 4.2.25, the set and env output is not escaped, so shell escapes and any non-printable characters won't be copy-pasteable. Take for example this shell session:
$ export foo=$'a\nbar=\baz'
$ ...
1
vote
1answer
182 views
What does mean ` AWK=@AWK@` in shell scripting
I found AWK=@AWK@ while reading sh script, it's something like AWK=$(which awk), but it's not working.
So can anyone explain this ?
Script :
#!/bin/sh -
# makelist.sh: Automatically generate header ...
1
vote
2answers
398 views
How do I set bash aliases and variables from within vim?
In cmdline mode, the following examples do not work for me.
!alias lol='echo lol'
!lol='echo lol'
2
votes
1answer
253 views
shell variable in awk is not being passed to all the lines but just for the very first line of input?
inputfile:is2.txt
10.39.5.41,A1,B1
10.39.5.41,A2,B2
10.39.5.41,A3,B3
10.39.5.41,A4,B4
10.39.5.41,A5,B5
10.39.5.41,A6,B6
script :
#!/bin/bash
second_column="OOOOOOO" # OOOOOOO will be added to ...
3
votes
2answers
201 views
Setting a shell variable in a null coalescing fashion
I'm really fond of "null coalescing", where you can set a variable to the first "non-null" value in a list of things. Many languages support this, for example:
C#:
String myStr = string1 ?? string2 ...