The variable tag has no usage guidance.
1
vote
3answers
38 views
Pipes vs variable assignment
In what ways are pipes different from variable assignments?
I know you can't use null bytes. What other examples are there?
What about for piping in something like the following yes | cat?
I notice ...
0
votes
1answer
41 views
grep a variable within an if statement
Maybe I am approaching this the wrong way, so I am open for suggestions. I am trying to avoid adding the output of INSTALLED_LIST to a file. I wish to use it within a variable.
Basically, I want to ...
0
votes
3answers
45 views
Ignore certain characters when comparing variables?
For example, If this is my script:
if [[ $yesno == "What's going on" ]]; then
Then how do I make it Ignore ', ?, , and . for example?
So basically Whats going on, What's going on, What's going on? ...
0
votes
2answers
32 views
Choose random string from a few and set it as a variable?
I have a few strings, and I want to set a variable to one of them, randomly. Say the strings are test001, test002, test003 and test004.
If I set it like normal I'd obviously do it like this:
...
0
votes
2answers
27 views
Date format for variable
I need to write a variable with "date" function and I need that the format is yyyymmdd (i.e. 20151031). Also I would remove a day (i.e. 20151031 becomes 20151030)
I would use bash.
Thanks!
1
vote
1answer
55 views
Get the output from expect script in a variable
I have an expect script which provides the IP address:
#!/bin/expect -f
set nodename [lindex $argv 0]
spawn virsh console $nodename
expect "Escape character is"
send "\n"
expect "localhost login: " {
...
0
votes
1answer
27 views
ssh pass multiple values
I want to open some ports i do
for i in centos7{3..4};do ssh -T $i "for ir in 53 80 443 3000 3306 5910-5930 5432 8140 8443 10090-10100 20 21;do firewall-cmd --permanent --zone=public ...
4
votes
2answers
103 views
Are there shells that supports typed variables and multidimensional arrays?
Is there a shell that supports distinguishing between variable types?
At a minimum, it should distinguish between Strings, Integers, Floats and Doubles.
The following commands should be different. ...
1
vote
2answers
108 views
Bash script to fill in a template
I have looked for this elsewhere but not able to find something that matches my requirements.
The template below is the one I need to use :
dn: uid=$1,ou=home,dc=chan,dc=com
objectClass: ...
1
vote
1answer
142 views
How to use long variables name in Makefile?
Today I'm learning make command, and I find it seems can execute any bash command by reading Makefile on current directory.
However, I encountered a problem. That when using variables, it seems ...
3
votes
3answers
96 views
How do I assign the output of a command to a variable without running the command in a subshell?
If I have a function like this:
TEST()
{
if [[ "$1" == "hi" ]]
then
exit 1
fi
echo "Some text"
}
and if I run the function in the current shell with:
TEST "hi"
...
1
vote
3answers
64 views
How to pass directory with escaped space to variable?
I have a small test.sh script as follows:
#!/bin/bash
read -e -p "Enter a Directory: " directory
echo $directory
Here's what happens when I run it:
$ ./test.sh
Enter a Directory: ~/A\ Directory/
...
1
vote
1answer
31 views
bash error on eval containing blank space
I need to triggeer a bash passing parameter with a format [ variable_name="value" ] like this:
./test.sh ip='164.130.21.98' hostname='whatever' pwd='/test' ftpcmd='CWD debug' user='stefano'
On ...
0
votes
2answers
40 views
Initializing a variable with echo
So, I'm trying to store a part of the name of a file into a variable for other uses. So, if my file was named hello123.txt and I want to store the 123 part. So I try
num= echo $file |cut -c6-8
...
1
vote
2answers
171 views
How to count how many ip in bash?
Does anybody know how to count how many ip in bash?
For example:
Command:
root@ubuntu:~$ dig www.google.com A +short | grep -oE ...
0
votes
0answers
24 views
How to reference a variable by its string “name”? [duplicate]
Say for example...
#!/bin/bash
a1=one a2=two a3=three
for x in {1..3}; { echo -n "\$a$x "; }
Actual..
$a1 $a2 $a3
Expected..
one two three
How can one echo, reference, call, or what ...
1
vote
1answer
100 views
Set variable inside heredoc, use variable outside heredoc
I've got the following in a bash script
ssh -T -i ${ssh_key} ${ssh_user}@${host} <<- 'EOF'
file=`hostname`.tar.gz
echo "file: " $file
### prints>> file: some-host-name.tar.gz
EOF
...
0
votes
2answers
38 views
variable inside a variable in a loop
I have a question regarding how a variable can be called with another variable name inside a loop. The following script does not work.
#!/bin/bash
# Comparing test1.txt with test2.txt, test1.ini with ...
1
vote
1answer
37 views
Indirectly expand variables in shell
I need to indirectly reference a variable in the bash shell.
I basically want to what you can do in make by writing $($(var)).
I have tried using ${$var} which would be the most straight forward ...
0
votes
2answers
60 views
How to increment local variable in Bash?
Data
1
\begin{document}
3
Code
#!/bin/bash
function getStart {
local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)"
echo $START
}
START2=$(getStart)
echo ...
0
votes
2answers
42 views
Returning local values from Bash variables?
I studied this article called Returning Values from Bash Functions.
Data
Lorem.
\begin{document}
hello
\end{document}
Case #1 which does not work
Code
#!/bin/bash
function getStart {
...
1
vote
2answers
167 views
How do I pass a variable into sftp?
I'm trying to match a filename to a string, which is basically a timestamp, during an SFTP connection.
How do I match part of the filename? I can't seem to declare a variable within an SFTP ...
0
votes
1answer
34 views
Issues with storing an echo of a date conversion into a string variable inunix
Apologies but this question comes from a question previously raised: How can I convert a local date-time into UTC date-time? Toby has been absolutely great helping me with some conversion string-->BST ...
0
votes
1answer
68 views
Parsing a file to determine what values to use [duplicate]
I have a script and a "config" file that pull variables from. I created the configuration file called config.cfg and used it in my script as such:
#! /bin/bash
if [ -f ./config.cfg ]
then
...
0
votes
2answers
41 views
Different behaviour of eval “ command \”$@\“ ”, “$@”, and “$(echo $@)” when used as arguments [duplicate]
I would like to pass multiple arguments to the Tor browser (firefox) programatically through a function arbitrarily entitled tor. This is so as to command tor search terms and voila! My search terms. ...
9
votes
3answers
358 views
Define variables with a variable name
What's wrong with this script?
I'm trying to define A1=1, B1=1, C1=1
LIST="A B C"
for x in $LIST
do
"$x"1=1
done
and the result is:
./x.: line 7: A1=1: command not found
./x.: line 7: B1=1: ...
4
votes
3answers
127 views
How to use call-by-reference on an argument in a bash function
I am trying to pass a "var name" to a function, have the function transform the value the variable with such "var name" contains and then be able to reference the transformed object by its original ...
1
vote
4answers
152 views
bash dynamic (variable) variable names
I want to dynamically create a sequence of strings by manipulate an array of elements and create some arithmetic procedure.
for name in FIRST SECOND THIRD FOURTH FIFTH; do
$name = $(( $6 + 1 ))
...
0
votes
1answer
87 views
How to use pipe to assign a variable [duplicate]
I am new to bash script. Probably this is very easy but for me it's not working. How can I assign value of $x1+$x2 to bc by piping. here in my code $bc holds no value.
echo -n 'value x1 : '
read x1
...
1
vote
2answers
118 views
Copy Single Value from a file to a variable and perform arithmetic over it? [duplicate]
Suppose we have a file like 'abc.txt' which has a single value 123456 and file 'xyz.txt' which also has a single value like 654321. I want to store these values form the file to some shell variable ...
1
vote
2answers
44 views
A variable in Bash that contains quotation marks and spaces
I'm trying the following in a Bash script:
MV_PARAMS='"foo 1" "foo 2"'
mv $MV_PARAMS
What I want to actually execute is:
mv "foo 1" "foo 2"
But it doesn't seem to work. trying this:
mv ...
-1
votes
1answer
61 views
How can I delay the evaluation of variables (lazy evaluation)?
For example, I create 32768 directories named 0,1,2,...32767.
I want to randomly choose one as the path every time I run a command.
So I change $PATH to $PATH:/blabla/$RANDOM, but it won't work ...
4
votes
1answer
68 views
“$@” expansion for user defined variables
I'm trying to get a (bourne shell) variable to expand like "$@" so it produces multiple words with some having preserved spaces. I've tried defining the variable in many different ways but still can't ...
0
votes
2answers
69 views
Variable expansion inside parentheses and quotes
In the script below, I can't seem to make $var1 expand in the second statement. I've tried $var1, ${var1}, echo $var1 and '$var1'. It is inside a few sets of quotes and parentheses which I guess is ...
1
vote
1answer
52 views
Loop over lines of file, partition file and classify each partition
In the following I've put up my code for iterating over lines of a file in order to collect data within a segment from the file in following fashion:
Illustration for understanding procedure:
segL ...
3
votes
2answers
37 views
Setting variable with default value behaves differently when preceding a command?
Setting a variable with a fallback default value works...as long as we're using the variable in a subsequent command:
$ unset APP_ENV
$ echo $APP_ENV
$ APP_ENV="${APP_ENV:-production}"; echo ...
3
votes
1answer
67 views
Store specific output to a shell variable
I am looking to store the output of the svn info command, more precisely the revision number into a variable for further processing. I just need the 4 stored. Can anyone help me please?
$ svn info
...
3
votes
3answers
144 views
What is the difference of defining a variable with or without quotation marks? [duplicate]
If I define a variable with the quotation mark:
TEMP="~/Dropbox"
then
ls $TEMP
would not work, instead
echo $TEMP | ls
works.
And to get the same result, I can also define the variable ...
1
vote
1answer
56 views
increment a value pulled from a file
I have a cron that I am writing that will repeat every hour, and I need a simple way to pass a counter from the last run to the next run.
My plan was to add the number to a file at the end, and then ...
2
votes
2answers
191 views
Assign variable identifier in loop and echo its value
I hope this question is not a duplicate because I've been trying various ways to solve this problem but couldn't find any solution.
I have a for loop in which I'd like to store a command output into ...
1
vote
0answers
23 views
Moving to FHS 3: How to overwrite GNU Autotools directory variables?
In GNU Autotools, the directory variables $sharedstatedir and $localstatedir expand to $(prefix)/com and $(prefix)/var respectively.
However the com and var directories don't exist anymore in FHS 3, ...
3
votes
1answer
209 views
Parameter expansion in variable assigned with a wildcard
I have two files in my current folder (MA502) whose names are -
MA502_TAAGGCGA-TCGCAGG_L001_R1_001.at.fastq
MA502_TAAGGCGA-TCGCAGG_L001_R2_001.at.fastq
I have many such folders - ex MA503, MA504 ...
2
votes
3answers
175 views
Split command and arguments, and put into variable in a bash script
I have a script that repeats the same (very long) grep command many times. I want to make the script more readable by cutting the line length, so I tried to put parts of the command into variables.
...
9
votes
6answers
4k views
How do I check if a variable exists in bash?
I need to check a variable's existence in an if statement. Something to the effect of:
if [ -v $somevar ]
then
echo "Variable somevar exists!"
else
echo "Variable somevar does not exist!"
...
0
votes
2answers
105 views
Replace spaces by underscores in files: Error in my bash script
I don't understand the error in my bash script.
#!/bin/bash
newFile=""
for file in *
do
newFile={$file/ /_} # so as to replace spaces with underscores
mv {$file} {$newFile}
done
It ...
0
votes
0answers
22 views
Run a command stored in a variable [duplicate]
I am working on updating some scripts we have used previously ( on RHEL) to work for a new flavor of Linux (ESX). As I am modifying the script, I wanted to add user feedback as well as some other ...
1
vote
2answers
98 views
concatenating file names in a variable
I am attempting concatenate file names to use in a ftp mdelete command. Each file name needs to be separated by a space in order for the mdelete command to work. The $i variable is in a loop and I ...
0
votes
2answers
332 views
Why are there three syntax for setting variables in shell scripts?
root@ubuntu:~# echo ${x=1}
1
root@ubuntu:~# echo ${x:=1}
1
root@ubuntu:~# echo ${x-1}
1
root@ubuntu:~#
Why are there three syntax for setting variables in shell scripts?
What is the technical ...
3
votes
6answers
870 views
How to keep last exit status after test
Is it possible to keep the last command exit status ($?) unaltered after a test?
E.g., I would like to do:
command -p sudo ...
[ $? -ne 1 ] && exit $?
The last exit $? should return the ...
1
vote
1answer
66 views
Insert variable in specific place using sed
Please help me i'm trying to insert variable in a file with sed but has not yet successfully((
I have variable $time and i want insert it in index.html file in specific string(line number 53):
the ...