Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to overwrite value of variable s1 from "abc" to ---> [more /home/mohsin/Desktop/system_usage_bash_file | grep TIME | awk 'BEGIN {FS=","} {print $3}']

but when i echo i get "abc" on both echos,means it is not overwritten

What I have tried:

#!/bin/bash
s1="abc"
s2="USED"
echo "before"
echo $s1
s1= more /home/mohsin/Desktop/system_usage_bash_file | grep TIME | awk 'BEGIN {FS=","} {print $3}'
echo "after"
echo $s1
Posted
Updated 16-May-16 7:53am

You need to write it that way:

root@server:~# s1="$(echo 5)"
root@vpn-1gb-nyc3-01:~# echo $s1
5


So you'll need to rewrite your code like this:

C#
s1= "$(more /home/mohsin/Desktop/system_usage_bash_file | grep TIME | awk 'BEGIN {FS=","} {print $3}')"
 
Share this answer
 
s1= more /home/mohsin/Desktop/system_usage_bash_file | grep TIME | awk 'BEGIN {FS=","} {print $3}'

As far as I remember shell scripts, you need to put the command in back quotes thus:
s1= `more /home/mohsin/Desktop/system_usage_bash_file | grep TIME | awk 'BEGIN {FS=","} {print $3}'`

See the section titled Command Substitution at bash(1): GNU Bourne-Again SHell - Linux man page[^].
 
Share this answer
 
Comments
Member 12485314 26-Apr-16 4:35am    
On putting back quotes i got error that command not found
Richard MacCutchan 26-Apr-16 4:42am    
Well I don't expect it was more, grep or awk.
Member 12485314 26-Apr-16 4:54am    
actually i am moving to file directory [more /home/mohsin/Desktop/system_usage_bash_file] than i am greping time and after that printing some value out of it
Richard MacCutchan 26-Apr-16 5:10am    
That makes no sense. The more command types the contents of a file on the console. Type the pipeline at the prompt and see what happens.
Jochen Arndt 26-Apr-16 4:58am    
Start entering the first command on the shell and continue by appending the additional commands:

more /home/mohsin/Desktop/system_usage_bash_file
more /home/mohsin/Desktop/system_usage_bash_file | grep TIME

and so on.

Once it is working, copy it to your shell script and use the backticks as suggested by Richard or use the $(command) format.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900