I have the while loop below that is using the variable pov. I need each line set to a variable that can be called in a connection string, but cant figure out how to create a loop to feed each line separately.
wanting if [ ! -z $pov] then .../shell to execute using $pov... fi for each line in seq_fy.txt
What I am working with:
cat seq_fy.txt | while read pov; do
echo "pov$((n++))=$pov"
###wanting "if [ ! -z $pov] then <execute> fi" for each line in seq_fy.txt
done
$ ./while_loop_only
pov0=
pov1=SPT_SEQ_010,FY15
pov2=SPT_SEQ_010,FY16
pov3=SPT_SEQ_020,FY15
pov4=SPT_SEQ_020,FY16
pov5=SPT_SEQ_030,FY15
pov6=SPT_SEQ_030,FY16
pov7=SPT_SEQ_040,FY15
pov8=SPT_SEQ_040,FY16
pov9=SPT_SEQ_050,FY15
pov10=SPT_SEQ_050,FY16
Looks like I have been way over-analyzing this....
basically I don't need anything with POV or exporting variables at all, just simply put the command inside the while loop and fed in $line, and seems to work as expected
cat fiename.txt | while read line; do
$owsdirectory"/hpm_ws_client.sh" processCalcScriptOptions "$appName" "$line" "$layers" "$stages" "" "$stages" "$stages" FALSE > "$appLogFolder""/""$line""_ProcessID.log"
done
Related
Totally new in bash programming and got a problem like this:
N empty files should be created. The file sum N and also the file name should be given from users via command line.
no loop and getopts should be used.
I've tried something like this which i found from google but it doesn't works. I got confused between linux and windows bash scripts. Hope someone can help me with the problem. Thank you!
#!/bin/bash
echo $N
:start
if [ $N > 0 ]
then
touch xyzfile_$N
$N = $N - 1
pause
goto start
else
then
echo "no data will be created"
fi
The command line and the result should be (for example if N=7) look like this:
command line:
./createfiles -n filename 7
expected result:
filename_1,filename_2,filename_3...filename_7
You can do this using a recursive shell function:
fn() {
# add some sanity checks to check parameters
touch "$1_${2}"
(($2 > 1)) && fn "$1" $(($2 - 1))
}
This part is a recursive call:
(($2 > 1)) && fn "$1" $(($2 - 1))
Where it basically calls itself by decrementing 1 from 2nd argument as long as $2 is greater than 1.
Then just call it as:
fn filename 7
It will create 7 files as:
filename_1 filename_2 filename_3 filename_4 filename_5 filename_6 filename_7
I ended up the question with a for-loop like this:
createfile() {
name="$1"
num="$2"
for((i=1;i<=num;i++))
do
touch "$1_${i}"
done
}
createfile $1 ${2}
It works great actually. And now I tried to solve the problem with getopts and it seems a function call doesn't work in the case option
The following bash script's goal is to read CSV file ( all_words.CSV ) and print parameters and values but I have very strange problem.
When I run the script all words parameters (word1-word8) was printed - until now every thing is fine!When I want to print as word1=$word1 outside of function then from some reason word1 not get the value?
Why all parameters (word1-word8) print the values in function, and when I want to print word1 outside the function then word1 is without value?
I tried with export command but it doesn’t help as; export word1=$word1
Please advice how it can be? What the problem here?
#!/bin/bash
read_csv ()
{
CSV_LINE=2
vars=()
c=1
while IFS=, read -ra arr; do
if ((c==1)); then
vars+=("${arr[#]}")
elif ((c==CSV_LINE)); then
for ((i=0; i<${#arr[#]}; i++)); do
declare ${vars[$i]}="${arr[$i]}"
done
fi
((c++))
done < all_words.CSV
echo CSV_LINE=$CSV_LINE
echo word1=$word1
echo word2=$word2
echo word3=$word3
echo word4=$word4
echo word5=$word5
echo word6=$word6
echo word7=$word7
echo word8=$word8
}
read_csv
echo word1=$word1
.
more all_words.CSV
word1,word2,word3,word4,word5,word6,word7,word8
&^#G TR /erfernfjer *&^NHY " "" ? / $#H,#Y^%" E "R$%*&*UJ,**U&^#%%#$^&// \\,^T%!#&^YG.+___KI*&HHTY,%%#$#!%^#&,P/\06E87*UHG11#
,edehu234##!&,~hum&T%6e4
example of script output:
./readWords_from_csv.bash
CSV_LINE=2
word1=&^#G TR / erfernfjer *&^NHY " "" ? / $#H
word2=#Y^%" E "R$%*&*UJ
word3=**U&^#%%#$^&//\\
word4=^T%!#&^YG.+___KI*&HHTY
word5=%%#$#!%^#&
word6=P/\06E87*UHG11#
word7=edehu234##!&
word8=~hum&T%6e4
word1=
man bash explains under declare:
When used in a function, declare makes NAMEs local, as with the local command.
declare -g ${vars[$i]}="${arr[$i]}"
# ^^
Use declare -g to declare a variable at global level in a function. From man bash:
declare [-aAfFgilrtux] [-p] [name[=value] ...]
[...] The -g option forces
variables to be created or modified at the global scope, even
when declare is executed in a shell function. It is ignored in
all other cases. [...]
Here is a simple demonstration of the -g flag (works as expected on GNU bash, version 4.2.37):
#!/bin/bash
function f() {
declare -g V
V="hello"
}
f
echo $V
Please advice ...
Better use printf:
printf -v "${vars[$i]}" "%s" "${arr[$i]}"
Although I'd suggest using an associative array instead. It's the more appropriate solution:
#!/bin/bash
declare -A CSV_VALUES
declare -a CSV_KEYS
function read_csv {
CSV_VALUES=() CSV_KEYS=()
local VALUES I
{
IFS=, read -ra CSV_KEYS
IFS=, read -ra VALUES
} < all_words.csv
for I in "${!CSV_KEYS[#]}"; do
CSV_VALUES[${CSV_KEYS[I]}]=${VALUES[I]}
done
}
read_csv ## Perhaps pass the filename to read_csv as an argument instead?
# We can do for KEY in "${!CVS_VALUES[#]}" but the order is uncertain.
for KEY in "${CSV_KEYS[#]}"; do
echo "CSV_VALUES[$KEY]=${CSV_VALUES[$KEY]}"
done
I wrote simple script as follow
#!/bin/bash
auth_type=""
SM_Read-only="Yes"
SM_write-only="No"
echo -e ${SM_Read-only}
echo -e ${SM_Write-only}
if [ "${SM_Read-only}" == "Yes" ] && [ "${SM_Write-only}" == "Yes" ]
then
auth_type="Read Write"
else
auth_type="Read"
fi
echo -e $auth_type
And when i execute it i got following output with errors.
./script.bash: line 5: SM_Read-only=Yes: command not found
./script.bash: line 6: SM_write-only=No: command not found
only
only
Read
Any one know correct way to declare the variable with "-" (dash)?
EDIT:
have getting response from c code and evaluate the variables for example
RESP=`getValue SM_ Read-only ,Write-only 2>${ERR_DEV}`
RC=$?
eval "$RESP"
from above scripts code my c binary getValue know that script want Read-only and Write-only and return value to script.So during eval $RESP in cause error and in my script i access variable by
echo -e ${SM_Read-only}
echo -e ${SM_Write-only}
which also cause error.
Rename the variable name as follows:
SM_Read_only="Yes"
SM_write_only="No"
Please, don't use - minus sign in variable names in bash, please refer to the answer, on how to set the proper variable name in bash.
However if you generate the code, based on others output, you can simply process their output with sed:
RESP=$(getValue SM_ Read-rule,Write-rule 2>${ERR_DEV}|sed "s/-/_/g")
RC=$?
eval "$RESP"
- is not allowed in shell variable names. Only letters, numbers, and underscore, and the first character must be a letter or underscore.
I think you cant have a dash in your variables names, only letters, digits and "_"
Try:
SM_Read_only
Or
SM_ReadOnly
I am trying to merge two very different scripts together for consolidation and ease of use purposes. I have an idea of how I want these scripts to look and operate, but I could use some help getting started. Here is the flow and look of the script:
The input file would be a standard text file with this syntax:
#Vegetables
Broccoli|Green|14
Carrot|Orange|9
Tomato|Red|7
#Fruits
Apple|Red|15
Banana|Yellow|5
Grape|Purple|10
The script would take the input of this file. It would ignore the commented portions, but use them to dictate the output. So based on the fact that it is a Vegetable, it would perform a specific function with the values listed between the delimiter (|). Then it would go to the Fruits and do something different with the values, based on that delimiter. Perhaps, I would add Vegetable/Fruit to one of the values and dependent on that value it would perform the function while in this loop to read the file. Thank you for your help in getting this started.
UPDATE:
So I am trying to implement the IFS setup and thought of a more logical arrangement. The input file will have the "categories" displayed within the parameters. So the setup will be like this:
Vegetable|Carrot|Yellow
Fruit|Apple|Red
Vegetable|Tomato|Red
From there, the script will read in the lines and perform the function. So basically this type of setup in shell:
while read -r category item color
do
if [[ $category == "Vegetable" ]] ; then
echo "The $item is $color"
elif [[ $category == "Fruit" ]] ; then
echo "The $item is $color"
else
echo "Bad input"
done < "$input_file"
Something along those lines...I am just having trouble putting it all together.
Use read to input the lines. Do a case statement on their prefix:
{
while read DATA; do
case "$DATA" in
\#*) ... switch function ...;;
*) eval "$FUNCTION";;
esac
done
} <inputfile
Dependent on your problem you might want to experiment with setting $IFS before reading and read multiple variables in 1 go.
You can redefine the processing function each time you meet a # directive:
#! /bin/bash
while read line ; do
if [[ $line == '#Vegetables' ]] ; then
process () {
echo Vegetables: "$#"
}
elif [[ $line == '#Fruits' ]] ; then
process () {
echo Fruits: "$#"
}
else
process $line
fi
done < "$1"
Note that the script does not skip empty lines.
I am having problems concatenate two strings in BASH (I am using Cygwin)
When I am doing it step by step in the cygwin window, it works.
i.e by defining dt=2012-12-31 and c=.txt explicitly and then concatenating in filename=${dt}${c}.
It doesn't seem to work when i am running it through my script where these variables are defined by cutting and assigning values from content of a file.
Though the variables are assigned with the same values as above, the concatenation in this case doesn't work.
instead of 2012-12-31.txt i am getting .txt-12-31 as result.
The code is:
for x in {0..11}
do
IFS=$'\n'
filename=date_list.txt
file=($(<"$filename"))
IFS=$'\t\n'
dt=${file[$x]}
echo $dt
for y in {0..85}
do
IFS=$'\n'
filename=SQL_Mnemonics.txt
file=($(<"$filename"))
IFS=$'\t\n'
Mn=${file[$y]}
for k in {3..502}
do
IFS=$'\n'
c=.txt
filename=${dt}${c}
file=($(<"$filename"))
IFS=$'\t\n'
echo ${file[$k]} > temp_file.txt
cusip=`cut -c11-19 temp_file.txt`
result=$(sh ctest.sh $Mn, $dt, $cusip)
echo "$result" > tmp1.txt
t1=`cut -c18-40 tmp1.txt`
echo $t1 | sed 's/[[:space:]]//g' > temp_file.txt
cat tst.txt | sed 's/-----//g' >> ForFame/${Mn}.${dt}.txt
done
done
done