2

i have a script named variable.sh and contain set of many variables as per below:

var1=variable1
var2=variable2
var3=variable3
var4=variable4
var5=variable5

I need to run a command which will change the position of the variables in variable.sh. and the variables will keep changed when i run the command.It will become as below:

First change:

var1=variable2
var2=variable3
var3=variable4
var4=variable5
var5=variable1

Second change:

var1=variable3
var2=variable4
var3=variable5
var4=variable1
var5=variable2

Is there any possible command to do the above changes? i am using bash script in ubuntu. thank you

0

1 Answer 1

1

This can be done for example using Bash arrays:

#!/usr/bin/env bash

inputfile=/path/to/input

vars=()
values=()
while read line; do
    vars+=(${line%%=*})
    values+=(${line#*=})
done < "$inputfile"

values+=(${values[0]})
for ((i = 0; i < ${#vars[@]}; i++)); do
    echo ${vars[i]}=${values[i + 1]}
done > "$inputfile"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.