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)
1
vote
1answer
29 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
32 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
35 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
52 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
26 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
29 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
105 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
188 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
15 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
17 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
293 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
52 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
714 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
25 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
35 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
28 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
24 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
25 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
37 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
55 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
23 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. ...
1
vote
1answer
38 views
find command not giving any output
Please anyone know why this command is not giving me any output? allthe variables are correct but no result.
find . -name "$cdr_type*$DAYZ*.unl*" |
xargs zcat |
awk -F "|" '{if($14==$tdate && $22=...
0
votes
1answer
92 views
Checking if string exists in file with cat | grep
Good day all
Purpose: checking if a string exists in a file and running actions accordingly
if [ -z 'sudo cat /etc/sudoers | grep "$USER ALL=(ALL) NOPASSWD:ALL"' ]; echo "no output, cont." || echo "...
0
votes
0answers
34 views
Variable Crossover in multiple bash scripts?
I have two bash scripts running simultaneously, which are identical apart from two things.
Apart from a few other setup pieces they run the following entry:
ssh ${target_server} "bash -s" < ${...
0
votes
2answers
46 views
Ksh compound variable referencing
I would like to use KSH feature called compound variable to show better what's going on in the code.
On the other hand my the interesting values are held in number organized array.
I would like to ...
0
votes
2answers
18 views
Setting my variable, do I use the dot notation or ~?
All my Hadoop configuration files are located in the following:
./.linuxbrew/Cellar/hadoop/2.7.3/libexec/etc/hadoop/"
To set my HADOOP_HOME variable, do I use the dot notation or ~?
HADOOP_HOME = "....
2
votes
1answer
41 views
I want to use the variable declared inside an SSH session to be used locally in my shell script
Whenever I try to use the variable declared inside my SSH session it gives me blank output. Here is the code which I am trying to execute:
ssh -T host <<\HERE
export usage1=$(df -h |grep /nas/...
3
votes
2answers
66 views
Remove specific word in variable
In a bash script, how can I remove a word from a string, the word would be stored in a variable.
FOO="CATS DOGS FISH MICE"
WORDTOREMOVE="MICE"
-1
votes
1answer
14 views
View local variables on CentOS
How can I view local variables? If I create a local variable e.g. VR1=rhe17 I'm not able to see it when running printenv unless I make it an environment variable using export VR1.
1
vote
2answers
38 views
Pass variable IP address to if else
I have this command:
ptr=`host $hostname`
Which results in this:
test.tester.test has address 192.168.1.1
This Works!
What I want now is to extract only the IP address (192.168.1.1), pass it
to ...
0
votes
1answer
35 views
linux + .bash_profile + correct PATH syntax [duplicate]
regarding the file: .bash_profile
what is the different between
PATH=${PATH}:${HOME}/bin:/usr/local/
to
PATH=$PATH:$HOME/bin:/usr/local
and what is the correct syntax?
2
votes
1answer
45 views
Appending a variable to different lines of a file in bash [duplicate]
So say I've got a file with the following text in it.
And the source file I'm reading is like this:
#0 abc 2016-08-06 01:12:57 AM 9.1% 779.9M of 1G 156.3M of 1G
#1 abc 2016-08-06 02:33:47 ...
1
vote
1answer
45 views
Setting environment variables by prompt instead of in command line
Is there a way for setting an environment variable in bash such that its value is not passed directly after = but is prompted for separately? For example, something akin to $ TEST=< (syntax does ...
1
vote
3answers
46 views
Extracting data from a variable at the command-line
I have two variables $dc_list and $TEST_VENUE . If I do echo in bash
echo $dc_list
{"active-production-dc":"sc-va1","standby-production-dc":"sc-tx2","active-integration-dc":"int-va1","standby-...
1
vote
2answers
247 views
Working with local and remote variables in ssh session
I'm writing a script that should scp me some files after they have been processed on a remote server. To do that I need variables from the local server that executes the script and later some ...
2
votes
1answer
35 views
Trim a text without loop
input (in variable as $VAR) :
'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'dum' : x'dum' 'test' : x'test' 'poo' : x'poo' 'D2-dog' : x'D2-dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass'
...
1
vote
1answer
39 views
Storing the each output into a variable
I have script here that will list the date that the user will enter and output the date 5 days ago.
#!/bin/bash
echo "What month?"
echo "1 - January"
echo "2 - February"
...
1
vote
1answer
58 views
Func name as variable in loop
Overview:
I save my variable in a config file and call them later.
Each entry with the name FailOverVM has a number beside it like FailOverVM1 and I want to check to see if it has data and generate a ...
0
votes
2answers
47 views
Preform operation in bash only if a variable is less than a second variable [closed]
I am building an OS for a virtual machine I made, and while progress has been coming along swimmingly, I have been stuck for a good several hours on what should be a very simple issue: I am designing ...
6
votes
2answers
404 views
Why does changing $PATH affect child shells, but changing $foo not?
$ unset foo
$ unset bar
$ echo $foo
$ echo $bar
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ foo=a
$ bar=b
$ export bar
$ echo $foo
a
$ ...
0
votes
3answers
37 views
Removing space at the beginning of the variable data
I would like to ask if how can I remove the space in the beginning of the stored data of the variable.
I had an auto-generated script that will get MySQL COUNT from the DB.
MYSQL COUNT SCRIPT-> .ini ...
1
vote
1answer
65 views
$($string) execution failure
I have $(${StrVar}), my StrVar has $ in value eg. test$9hp.
When I execute the command: $(${StrVar} it fails as $9 is inside my variable and it tries to expand it.
function Deploy
{
# in this ...
0
votes
2answers
68 views
Define a set of values for a variable in shell script
I want to define a set of values for a single variable. E.g.
singleDigit={0,1,2,3,4,5,6,7,8,9}
If the user gives 10 it should exit with an error.
How can this be achieved without using a loop, but ...
3
votes
3answers
164 views
Could not execute a Bash Script if / while
I would like if someone points out mistakes in my script. The source where I'm learning from is so buggy that's why it's confusing me.
PURPOSE OF THIS SCRIPT: It will count the numbers from ...
1
vote
1answer
61 views
Save return value from a function in one of its own parameters
This is what I want to achieve:
Function:
Func1() {
$1="Hello World"
}
Call function:
local var1
Func1 var1
echo $var1 (should echo Hello World)
I found this example which seems to work, but ...
1
vote
1answer
40 views
Output in variable shouldn't be interpreted as command [duplicate]
So this question was asked some times already, but the answers didn't seem to work for me. So I have this simple script
#!/bin/bash
charon_id = $(pidof charon)
kill -1 $charon_id
And console says ...
0
votes
1answer
43 views
Transferring a variable from one file to another
I have two files, config and template, and I want replace parts of the template with variables from config:
config.txt contains:
MYURL='www.google.com'
template.txt contains:
url = MYURL
I used
...
0
votes
1answer
90 views
Assign hostname to a variable in ksh
I am trying to assign the output of hostname to a variable and use it in my script. While I was able to get the variable to another logic, when I try to print the variable it doesn't print the ...