Hi all.. Esalam alakom
Hoping you are in safe
recently learned/discovered/had to use 2 trick
1- inline if statement (please check line 7)
2- i am writing a script that generate bash commands and when assigning a
variable that have $() and then echo it... it execute it , to solve that
put the skip character \ (please check line 11 & 12)
[abdo@alma-01 script]$ cat -n script.sh
1 #! /bin/sh -x
2
3 vg=tawdyVG
4 lv=abdoLV
5 error=""
6 genfile=file.txt
7 [[ $(id -u) -ne 0 ]] && error=$error+' you are not root'
8
9
10
11 Line1=" ## no_disk=\$(vgs $vg -o pv_count| tail -1)"
12 Line2=" ## lvcreate -i \$no_disk -n $vg -L 20G $lv"
13
14 if [[ "$error" = "" ]]; then
15 echo "$Line1" > $genfile
16 echo "$Line2" >> $genfile
17 else
18 echo "##!! there is error:$error" >$genfile
19 fi
20
[abdo@alma-01 script]$ ./script.sh
+ vg=tawdyVG
+ lv=abdoLV
+ error=
+ genfile=file.txt
++ id -u
+ [[ 1000 -ne 0 ]]
+ error='+ you are not root'
+ Line1=' ## no_disk=$(vgs tawdyVG -o pv_count| tail -1)'
+ Line2=' ## lvcreate -i $no_disk -n tawdyVG -L 20G abdoLV'
+ [[ + you are not root = '' ]]
+ echo '##!! there is error:+ you are not root'
[abdo@alma-01 script]$ cat file.txt
##!! there is error:+ you are not root
[abdo@alma-01 script]$ sudo ./script.sh
+ vg=tawdyVG
+ lv=abdoLV
+ error=
+ genfile=file.txt
++ id -u
+ [[ 0 -ne 0 ]]
+ Line1=' ## no_disk=$(vgs tawdyVG -o pv_count| tail -1)'
+ Line2=' ## lvcreate -i $no_disk -n tawdyVG -L 20G abdoLV'
+ [[ '' = '' ]]
+ echo ' ## no_disk=$(vgs tawdyVG -o pv_count| tail -1)'
+ echo ' ## lvcreate -i $no_disk -n tawdyVG -L 20G abdoLV'
[abdo@alma-01 script]$ cat file.txt
## no_disk=$(vgs tawdyVG -o pv_count| tail -1)
## lvcreate -i $no_disk -n tawdyVG -L 20G abdoLV