The tag has no wiki summary.

learn more… | top users | synonyms

5
votes
1answer
152 views

ifconfig -a in a shell script

I am writing a shell script to start my network in my virtual machine on boot, since it does not do this right away for some reason with a snapshot of a virtual machine. Since the eth device starts ...
2
votes
1answer
62 views

Could “>” be included in a bash Variable?

Current approach: sudo awk 'BEGIN{printf "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<hello>\n\t<world>\n";} {print "\t\t",$0}END{printf "\t </world>\n</hello>\n"}' ...
1
vote
1answer
39 views

command not found in shell script

Please tell me why I couldn't print variable value. # chmod 777 chkscript.sh # ./chkscript.sh chkscript.sh file content variable = "This is variable" echo "$variable" echo "Hello World " ...
1
vote
1answer
24 views

Insert variable and keep the variable

I'm on VMWare which, no matter what anyone says, responds to most unix commands. Some things are unique though, for example the fact that almost no files are persistent through reboot. Due to this ...
2
votes
1answer
132 views

What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts? [duplicate]

What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts? For example: filename=/etc/hosts if [ -r "$filename" ] && [ -s "$filename" ]; then md5sum $filename ...
2
votes
3answers
36 views

Reuse similar flags for multiple commands

Let's say I want to execute several commands with similar flags such as: du -sh / --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4 tar cvf archive / --exclude=dir1 --exclude=dir2 ...
0
votes
2answers
32 views

list files and store it in variables

im new with shell programming. my goal is to give the user a selection which file should be run. i catch all available scripts in one var with: var=$(find script*) so far so good.. this is the ...
0
votes
1answer
66 views

Bash Script : Passing a variable to a bash script that contains quotes, single quotes. etc [closed]

lets assume this is the string: 'a',"b" it contains both single and double quotes. how would you pass this to a bash script as a single string ? this is the bash script: #!/bin/bash echo $1 ...
1
vote
0answers
23 views

How to combine Bash's read with HERE-document when shopt -os errexit is in place? [duplicate]

I am trying to make use of the solution presented in one of the answers of http://stackoverflow.com/questions/1167746/how-to-assign-a-heredoc-value-to-a-variable-in-bash In Bash version ...
4
votes
1answer
128 views

Variable scope in while-read-loop on Solaris

    Could someone please explain to me why my while loop seems to have an internal scope? I've seen multiple explanations online but they all have to do with pipes. My code has ...
5
votes
1answer
65 views

What else can break if I remove export from a variable?

My problem is that when I started coding my script I encountered several variable scope issues which resulted in the bad habit of exporting almost all my variables. Now that my code has got quite ...
5
votes
5answers
207 views

Serialize shell variable in bash or zsh

Is there any way to serialize a shell variable? Suppose I have a variable $VAR, and I want to be able to save it to a file or whatever, and then read it back later to get the same value back? Is ...
2
votes
1answer
58 views

Value assigned inside a function variable is alway empty

I have a following simple script. In this script, I am assigning value to a global variable inside a function. I can clearly see that the value being assigned to the variable via debug statement. ...
1
vote
2answers
195 views

How do I get a variable's value from one script and import it in another script?

How do I get a variable's value from one script and import it in another script? But the catch is that the script where I am going to get the variable's value has also some commands that I don't want ...
0
votes
2answers
40 views

How to create a variable $ldir at your home directory that contains the relative path to letters?

If there's a directory on your Desktop called letters: 1) How to create a variable $ldir at your home directory that contains the relative path to letters? 2) How to copy a text file f1.txt to the ...
2
votes
1answer
47 views

Capture symbolic link into Unix/Linux variables

Say I have a folder with a series of symbolic links: ussdv103:en195d; default$ pwd /oradba/app/oracle/admin/database1 ussdv103:en195d; default$ ls -ltr exp -> ...
1
vote
1answer
115 views

Using a Counter Loop for Environment Variable

I'm trying to write a code that moves files to a .junk directory using the built-in environment variables. My goal is that all a user would have to do is type sh junk filename1 filename2 filename3... ...
8
votes
4answers
690 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
655 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
1k 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
336 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
117 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
940 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
25 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
114 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
58 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
94 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 ...
2
votes
2answers
60 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
99 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: ...
8
votes
1answer
764 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
1k 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
167 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
267 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
1k 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
104 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
112 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
65 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
181 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
82 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
117 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
224 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
85 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
242 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: ...
3
votes
2answers
9k 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
510 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
262 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
500 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
92 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
577 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
861 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 ...