A variable is a name, if correctly chosen with a symbolic meaning, that holds a value or values. Use this tag if your question is specific on the use of variables on shell scripting (if you want to ask about variables in programming languages you should probably ask on StackOverflow)
4
votes
3answers
248 views
Why doesn't the conditional work when I use it directly without a variable?
This gives me an error that says too many arguments:
if [ $( file -b $i ) == "directory" ]
But when I tried this
name=$( file -b $i )
if [ name == "directory" ]
It seems to work just fine.
Can ...
3
votes
2answers
57 views
Variable from concatentation of variables [duplicate]
I think this is a rather simple question but, I cant make this work:
I have a whole lists of variables eg:
SP60=OLA SP61=BYE SP62=TRT
I want to create a loop to call them in the specific number of ...
0
votes
1answer
28 views
Set newly created file as variable in makefile [on hold]
I am trying to create a file and set the contents of a file to a variable but the variable is always seen as empty.
An example:
define FILE_VAR
cat /output/1.txt >> /output/2.txt
$(...
1
vote
1answer
32 views
how to add command with new line in the variables? [duplicate]
as you know when we writing list=$(ls -l) all the files added to $list like this :
total 0 drwx------ 7 mine-pc staff 238 Dec 22 11:32 Applications drwxr-xr-x@ 6 mine-pc staff 204 Jan 11 11:00 ...
0
votes
1answer
48 views
Using eval in make file
I run a buildroot make and during this build process a text file is created. After this happens another make as part of the build is run. I want to read the contents of the generated file into a ...
0
votes
0answers
22 views
Make, setting variables from file, assignment issues [duplicate]
I had the following code working when defined at the top of my make file, it reads the contents of a file and parses the text into variables correctly:
VER_VAR:=$(shell cat output/target/version.txt)
...
1
vote
4answers
65 views
Bash - assign array into variable as string
I have this code, it prints the correct result, but I can't figure out how to get the echo from the last line into a variable.
# hostname is 'tech-news-blog-324344' . Setting it into array ...
-1
votes
1answer
38 views
How to clear all bash variables? [duplicate]
I like to make command-line bash variables for testing or for other purposes. I give them generic names like a, b, var1... Sometimes I may forget which variables I've already used. Anyway, is there a ...
0
votes
1answer
52 views
How to use ssh in a function(bash)?
I am trying to execute few commands in a server by logging in using sshpass command like below.
SSH_ARGS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q'
sshpass -p 'password' ssh ${...
1
vote
1answer
40 views
Capture the output of a shell function without a subshell
I have rbenv (ruby version manager) installed on machine and it works like that:
$ rbenv local
2.3.1
Writing to stdout the local version of my ruby. I want to rescue this version and declare it in a ...
0
votes
2answers
42 views
Replace string in a file from bash script value
This bash script runs on a Mac terminal, it needs to ask the user for input $name, then replace a string in another file to include the user input PLACEHOLDER_BACKEND_NAME=$name.
#!/bin/bash
read -r ...
5
votes
2answers
165 views
How to insert variables inside a string containing “”?
I want to construct an xml string by inserinting variables:
str1="Hello"
str2="world"
xml='<?xml version="1.0" encoding="iso-8859-1"?><tag1>$str1</tag1><tag2>$str2</tag2&...
0
votes
1answer
42 views
How can I get `expect` to ignore $ (dollar sign) in a password?
I'm running an expect script and the password has a $ character. The shell looks for the following characters to expand into something and spits out no such variable.
Is there any way around this?
0
votes
1answer
32 views
How to loop through variables
I have a command in my csh script that can give at least 2 lines of output but may be more.
I am turning these lines into separate variables that I then want to pass to another command. How do I set ...
19
votes
5answers
2k views
Variable randomly chosen among three numbers (10, 100, and 1000)
Is it possible to have variable which picks a random number from three pre-decided numbers?
Sample:
var= 10 or 100 or 1000
0
votes
0answers
37 views
Using sed in shell script with multiline variables
I use shell scripts to setup different types of VMs. Often these scripts include multiline variables that need to be inserted into config files at certain positions using sed.
If I create them like ...
1
vote
1answer
128 views
Bash: Syntax error near unexpected token `)'
I have created a shell script that uses information stored in a config file. The issue I am facing is when I try to pass certain values stored in the config file, for use by the shell script, I get a ...
6
votes
2answers
311 views
Create Script from Bash Script using here-document; why does my variable get replaced?
I have a problem, while creating a script from a master one:
As simple as:
cat pippo << EOF
LOGFILE=test.log
echo '#############################' >> $LOGFILE
EOF
. But, when I ...
0
votes
2answers
36 views
One line env variable set behavior [duplicate]
If I do this:
SUMAN_DEBUG=foo echo $SUMAN_DEBUG
I get no output
but if I do:
SUMAN_DEBUG=foo && echo $SUMAN_DEBUG
then I get
"foo"
why is that?
2
votes
1answer
35 views
grab from a text file a range using two variables as the beginning and end parameters
I'm trying to grab a specific range of lines from a text file using grep; the line range needs to be identified by two variables. so far, I've been trying to do it using the wildcard '*' examples of ...
0
votes
2answers
38 views
new lines and bash variable
I have a text file (or pipe output, doesn't matter here)
memcached.uptime 1061374
memcached.curr_connections 480
memcached.cmd_get 478962548
memcached.cmd_set 17641364
memcached.cmd_flush 0
If I use ...
0
votes
2answers
37 views
Multiple variable storage in a plain text database format
I've got some command outputs saved into variables in bash script, for instance 3, in each loop iteration. I'd like to save these variables inside a plain text database containing 3 fields before the ...
1
vote
3answers
46 views
How to match text between a string and a space
I have a simple bash script that reads lines from a text file like so:
#!/bin/bash
FILE=$1
while read line; do
done < $FILE
I would like to match text between the string "-type " and a space, so ...
-1
votes
3answers
47 views
How to assign a string with multiple spaces to a variable in bash?
First its not like this question
How do I echo a string with multiple spaces in bash “untouched”? [duplicate] because in that question he just want to print it and I want to assign it to variable and ...
0
votes
1answer
32 views
sed in bash scripting troubleshooting
I've got the following problem regarding to sed command. What I wish to do is:
sed '1 d' filename.fa | sed "1i\>filename\n" > filename_Edited.fa
where the file frist line is replaced by ">...
0
votes
3answers
127 views
rm command in bash script does not work with variable
I am doing a bash script to create a back-up of my mysql databases.
Actually, I would like to delete the old ones.
So, I create my script with variable for maintability.
The whole script is actually ...
4
votes
3answers
403 views
Unable to assign output of nested commands to variable in bash
I was trying to assign below command(which choose the random line from file) to a variable, but not working.
givinv@87-109:~$ head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1
cower
givinv@...
1
vote
1answer
38 views
Declare as local var will break a function and log out “1: number expected”
function projectopen {
local di_files=(*.xcworkspace */*.xcworkspace *.xcodeproj */*.xcodeproj)
# open first exsit file
ls -d -f -1 $di_files 2>/dev/null \
| head -1 \
| xargs ...
1
vote
1answer
48 views
Pass Shell variable to awk [duplicate]
I realize this has been asked many times and many of the answers seam well written, but I'm missing something with my little task. Trying to pass a Shell variable (an argument in this case) to awk to ...
-1
votes
1answer
38 views
For loop with if else using unlimited variables
I am currently working with some of the functions in bash. But as of now I am very confused. Let me explain first what I want to do.
Target Output:
2016-11-18T05:48:01 NFS="NFS1" MOUNT_STATUS=True ...
3
votes
3answers
62 views
What situations exist where Bash variables should not be double quoted? [duplicate]
I'm aware of several situations where it's unnecessary to use double quotes when expanding a variable in Bash, such as inside [[...]], or when used on the right hand side of a variable assignment.
...
1
vote
2answers
32 views
Why a value of a variable doesn't change inside backticks?
Here's my test code:
a=1
echo $a
echo `let ++a`
echo $a
The output that I see is 1, 1. Why doesn't the third line modify the value of a?
0
votes
1answer
36 views
How to include $ sign as string?
I want to create multiple files with a common template, problem is there have "$(variable)" in the template, which should be written with $ sign.
for example, I want output is $job_ID from
echo "...
3
votes
2answers
134 views
Store netcat output into variable
I want to store the output from an netcat function into a variable. I tried a lot of different ways, but it doesn't work to me. Can someone help me?
The whole scripting thing is whole new for me!
#! ...
3
votes
2answers
242 views
`read` command not working in a Makefile
I have a make script to perform 3 tasks:
Import a MySQL database
Move a configuration file
Configure the configuration file
For these tasks, the script requires 3 inputs:
MySQL Host
MySQL Username
...
0
votes
2answers
19 views
How to compare a command with an integer?
For example I declare a variable, which should return a current hour
var=`date +'%H'`.
And then I would like to compare it in if-statement if ["$var" -eq "23"]; then echo "$var = 23"; fi
But it doesn'...
1
vote
2answers
18 views
How to insert variable in get date expression?
I've given a variable which hold a number
daysAgo=1
I would like to expand this variable in a get date expression. Like this:
$(date +%d -d '$daysAgo days ago')
What do I need to do that the $...
-3
votes
2answers
30 views
Value of the $# variable [closed]
set 10 20 30 40 50
Enter the value of the variable $#:
I understand that the variables $1=10 $2=20 and so on
3
votes
3answers
306 views
How to use a variable for a line of code of an script?
I don't know how to use variables for further executing in a script.
I tried the following:
#!/bin/bash
NUM = 0
echo Number $NUM > text.txt
but I get the following error:
num.sh: 3: num.sh: ...
0
votes
2answers
73 views
Change exit code from 0 to 1 in bash script
I have a script that rsyncs and ends like this
rsync -azvh /dir -e ssh [email protected]:/
RESULT="$?"
# check result of rsync db's
if [ "$RESULT" != "0" ]; then
echo -e "rsync exit Code:" $RESULT "...
11
votes
8answers
728 views
Reversing a variable's contents by words
So if I've got a variable
VAR='10 20 30 40 50 60 70 80 90 100'
and echo it out
echo "$VAR"
10 20 30 40 50 60 70 80 90 100
However, further down the script I need to reverse the order of this ...
1
vote
1answer
36 views
How to make variable be taken into account [duplicate]
#!/bin/bash
awk 'NR!~/^(1|$q+2|$q+3)$/' deltay.txt > yota.txt
q is an integer obtain from a previous process in the script, and I need to erase lines one, the q+2 an q+3, but that command just ...
1
vote
0answers
27 views
Iterate over array which name is in another variable in bash [duplicate]
I want to iterate over an array which name is in another variable. This is what I tried so far:
NTASKSLIST=(384 768 1536 3072)
# there are potentially more arrays like these, each custom defined for ...
3
votes
1answer
36 views
awk a variable as a regex
The question is really simple, I have read all the questions and still cannot make it! I have a plain file like this
$cat file1.txt
ALA
AJD
KSF
And I want awk to use each of the values as regex to ...
-1
votes
1answer
34 views
Variables with equations
How would I create a variable that solves an equation.
For example,
t=20
s=(t+30)
I got the t=20 pretty easy but when it came to using the echo command for s, the output ends up being 20+30 instead ...
0
votes
1answer
26 views
Change where newly created RPM's are stored
Fairly new to creating RPM's but I have the basics down. I am working on scripting the basics and have a requirement where I cannot accept the default location for where the newly created RPM's will ...
1
vote
1answer
42 views
Shell variable in <a href> tag
I wrote a simple bash script, those send mail with html link.
#!/bin/bash
ASS=10
sendmail [email protected] <<EOF
Subject: Simple title
<html>
<body>
this is just a link
URL: <a ...
-2
votes
2answers
44 views
need help in shell shell scripting with oracle
I am trying to substitute a unix variable to a select statement, but I am getting the below error. What did I do wrong?
sqlplus "/ as sysdba" << EOF
spool /home/oracle/l.log
archive log list;
...
0
votes
2answers
92 views
how to understand the output of “echo $-”?
How to understand the output of echo $-? It looks like some kind of flag characters. I can't get a clue by googling.
0
votes
1answer
25 views
sed won't recognize numbers when passed as variables
This expression:
FN=27
LN=145
cat $FILENAME | sed -n $FN,$LNp
is failing with this error: "sed: -e expression #1, char 3: unexpected `,'"
but when I used actual numbers, I get the expected results. ...