AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1468482
Accepted
Brian
Brian
Asked: 2019-08-07 15:26:02 +0800 CST2019-08-07 15:26:02 +0800 CST 2019-08-07 15:26:02 +0800 CST

根据 Bash 中尚不存在的另一个变量分配变量值

  • 772

对于 BASH 脚本,我需要将另一个尚不存在的变量的值分配给脚本中较早的变量。但是,在为后面的变量赋值之前,我不需要调用前面的变量。

基本上,我需要第a32 行(以及其他实例)中的变量 来获取分配给另一个变量的值,该变量t直到第 132 行才存在(并且其值将随着循环而改变)。变量a在t被赋值之前不会被调用。我想我可以在第 135 行之后找到带有第二组 CASE 语句的替代解决方案,但如果我想要完成的事情是可能的,那将节省我的时间,这将是一种新技术,我也可以为未来的脚本添加我的工具带.

那么,我想做的是可能的吗?如果可以,我该如何完成。提前致谢。

1   IFS=$'\n'                               
2                                   
3   for i in {Gamma,Saturation,Brightness,Contrast}; do                             
4       for j in {Gamma,Saturation,Brightness,Contrast}; do                         
5           for k in {Gamma,Saturation,Brightness,Contrast}; do                     
6               for m in {Gamma,Saturation,Brightness,Contrast}; do                 
7                                   
8                   if [[ $i == $j || $i == $k || $i == $m || $j == $k || $j == $m || $k == $m ]]; then             
9                       continue            
10                  fi              
11                                  
12                  count=1             
13                                  
14                  s1=""               
15                  e1=""               
16                  i1=""               
17                  s2=""               
18                  e2=""               
19                  i2=""               
20                  s3=""               
21                  e3=""               
22                  i3=""               
23                  s4=""               
24                  e4=""               
25                  i4=""               
26                                  
27                  case $i in              
28                      Gamma)          
29                          s1=0.80     
30                          e1=1.20     
31                          i1=0.20     
32                          a=$t        
33                          ;;      
34                      Saturation)         
35                          s1=-20      
36                          e1=20       
37                          i1=20       
38                          b=$t        
39                          ;;      
40                      Brightness)         
41                          s1=-15      
42                          e1=15       
43                          i1=5        
44                          c=$t        
45                          ;;      
46                      Contrast)           
47                          s1=-30      
48                          e1=60       
49                          i1=10       
50                          d=$t        
51                          ;;      
52                  esac                
53                  case $j in              
54                      Gamma)          
55                          s2=0.80     
56                          e2=1.20     
57                          i2=0.20     
58                          a=$x        
59                          ;;      
60                      Saturation)         
61                          s2=-20      
62                          e2=20       
63                          i2=20       
64                          b=$x        
65                          ;;      
66                      Brightness)         
67                          s2=-15      
68                          e2=15       
69                          i2=5        
70                          c=$x        
71                          ;;      
72                      Contrast)           
73                          s2=-30      
74                          e2=60       
75                          i2=10       
76                          d=$x        
77                          ;;      
78                  esac                
79                  case $k in              
80                      Gamma)          
81                          s3=0.80     
82                          e3=1.20     
83                          i3=0.20     
84                          a=$y        
85                          ;;      
86                      Saturation)         
87                          s3=-20      
88                          e3=20       
89                          i3=20       
90                          b=$y        
91                          ;;      
92                      Brightness)         
93                          s3=-15      
94                          e3=15       
95                          i3=5        
96                          c=$y        
97                          ;;      
98                      Contrast)           
99                          s3=-30      
100                         e3=60       
101                         i3=10       
102                         d=$y        
103                         ;;      
104                 esac                
105                 case $m in              
106                     Gamma)          
107                         s4=0.80     
108                         e4=1.20     
109                         i4=0.20     
110                         a=$z        
111                         ;;      
112                     Saturation)         
113                         s4=-20      
114                         e4=20       
115                         i4=20       
116                         b=$z        
117                         ;;      
118                     Brightness)         
119                         s4=-15      
120                         e4=15       
121                         i4=5        
122                         c=$z        
123                         ;;      
124                     Contrast)           
125                         s4=-30      
126                         e4=60       
127                         i4=10       
128                         d=$z        
129                         ;;      
130                 esac                
131                                 
132                 for t in `seq $s1 $i1 $e1`; do              
133                     for x in `seq $s2 $i2 $e2`; do          
134                         for y in `seq $s3 $i3 $e3`; do      
135                             for z in `seq $s4 $i4 $e4`; do  
136                                 
137                                 if [[ $s1 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; t=$k; fi
138                                 if [[ $s2 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; x=$k; fi
139                                 if [[ $s3 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; y=$k; fi
140                                 if [[ $s4 == "0.80" ]]; then k=`printf "%0.2f\n" $t`; z=$k; fi
141                                 
142                                 echo $a $b $c $d $t $x $y $z
143                                 echo "Color_""$a""_""$b""_""$c""_""$d"".tif"
144                                 echo "Color_Glossy_""$i""_""$j""_""$k""$m""/"
145                                 echo ${i:0:1}" "$t" "${j:0:1}" "$x" "${k:0:1}" "$y" "${m:0:1}" "$z".tif"
146                                 echo ""
147                                 sleep 0.1
148                             done    
149                         done        
150                     done            
151                 done                
152             done                    
153         done                        
154     done                            
155 done                                

虽然我仍然很想知道我最初的问题是否可以按照我描述的方式解决,但这是我的解决方案,以防任何偶然发现这个问题的人也想看到解决方案——即使不是我寻求的方式。

IFS=$'\n'

for i in {Gamma,Saturation,Brightness,Contrast}; do
    for j in {Gamma,Saturation,Brightness,Contrast}; do
        for k in {Gamma,Saturation,Brightness,Contrast}; do
            for m in {Gamma,Saturation,Brightness,Contrast}; do

                if [[ $i == $j || $i == $k || $i == $m || $j == $k || $j == $m || $k == $m ]]; then
                    continue
                fi

                mkdir "Color_Glossy_""$i""_""$j""_""$k""_""$m"

                count=0001

                case $i in 
                    Gamma)
                        s1=0.80
                        e1=1.20
                        i1=0.20
                        ;;
                    Saturation)
                        s1=-20
                        e1=20
                        i1=20
                        ;;
                    Brightness)
                        s1=-15
                        e1=15
                        i1=5s
                        ;;
                    Contrast)
                        s1=-30
                        e1=60
                        i1=10
                        ;;
                esac
                case $j in 
                    Gamma)
                        s2=0.80
                        e2=1.20
                        i2=0.20
                        ;;
                    Saturation)
                        s2=-20
                        e2=20
                        i2=20
                        ;;
                    Brightness)
                        s2=-15
                        e2=15
                        i2=5
                        ;;
                    Contrast)
                        s2=-30
                        e2=60
                        i2=10
                        ;;
                esac
                case $k in 
                    Gamma)
                        s3=0.80
                        e3=1.20
                        i3=0.20
                        ;;
                    Saturation)
                        s3=-20
                        e3=20
                        i3=20
                        ;;
                    Brightness)
                        s3=-15
                        e3=15
                        i3=5
                        ;;
                    Contrast)
                        s3=-30
                        e3=60
                        i3=10
                        ;;
                esac
                case $m in 
                    Gamma)
                        s4=0.80
                        e4=1.20
                        i4=0.20
                        ;;
                    Saturation)
                        s4=-20
                        e4=20
                        i4=20
                        ;;
                    Brightness)
                        s4=-15
                        e4=15
                        i4=5
                        ;;
                    Contrast)
                        s4=-30
                        e4=60
                        i4=10
                        ;;
                esac

                for t in `seq $s1 $i1 $e1`; do
                    for x in `seq $s2 $i2 $e2`; do
                        for y in `seq $s3 $i3 $e3`; do
                            for z in `seq $s4 $i4 $e4`; do

                                if [[ $s1 == "0.80" ]]; then q=`printf "%0.2f\n" $t`; t=$q; fi
                                if [[ $s2 == "0.80" ]]; then q=`printf "%0.2f\n" $x`; x=$q; fi
                                if [[ $s3 == "0.80" ]]; then q=`printf "%0.2f\n" $y`; y=$q; fi
                                if [[ $s4 == "0.80" ]]; then q=`printf "%0.2f\n" $z`; z=$q; fi

                                case $i in 
                                    Gamma)
                                        a=$t
                                        ;;
                                    Saturation)
                                        b=$t
                                        ;;
                                    Brightness)
                                        c=$t
                                        ;;
                                    Contrast)
                                        d=$t
                                        ;;
                                esac
                                case $j in 
                                    Gamma)
                                        a=$x
                                        ;;
                                    Saturation)
                                        b=$x
                                        ;;
                                    Brightness)
                                        c=$x
                                        ;;
                                    Contrast)
                                        d=$x
                                        ;;
                                esac
                                case $k in 
                                    Gamma)
                                        a=$y
                                        ;;
                                    Saturation)
                                        b=$y
                                        ;;
                                    Brightness)
                                        c=$y
                                        ;;
                                    Contrast)
                                        d=$y
                                        ;;
                                esac
                                case $m in 
                                    Gamma)
                                        a=$z
                                        ;;
                                    Saturation)
                                        b=$z
                                        ;;
                                    Brightness)
                                        c=$z
                                        ;;
                                    Contrast)
                                        d=$z
                                        ;;
                                esac

                                old="Color_""$a""_""$b""_""$c""_""$d"".tif"
                                new="Color_Glossy_""$i""_""$j""_""$k""_""$m""/"$count" "${i:0:1}" "$t" "${j:0:1}" "$x" "${k:0:1}" "$y" "${m:0:1}" "$z".tif"

                                cp $old $new

                                count=$((10#$count+1))
                                countTmp=`printf "%04d\n" $count`
                                count=$countTmp
                            done
                        done
                    done
                done
            done
        done
    done
done
bash shell-script
  • 1 1 个回答
  • 148 Views

1 个回答

  • Voted
  1. Best Answer
    Kamil Maciorowski
    2019-08-07T22:42:54+08:002019-08-07T22:42:54+08:00

    来自Bash 参考手册:

    参数展开的基本形式是${parameter}. […]

    如果参数的第一个字符是感叹号 ( !) 而parameter不是 nameref,则它引入了一个间接级别。bash使用扩展其余参数形成的值作为新参数;然后将其扩展,该值将用于扩展的其余部分,而不是原始参数的扩展。这称为间接扩展。该值受波浪号扩展、参数扩展、命令替换和算术扩展的约束。[...] 感叹号必须紧跟在左大括号之后,以引入间接性。

    所以而不是

    a=$t
    

    你分配

    a=t
    

    然后像这样检索值:

    ${!a}
    

    例子:

    $ a=t
    $ echo "$a"
    t
    $ echo "$t"
    
    $ echo "${!a}"
    
    $ t=5
    $ echo "${!a}"
    5
    $ t=foo
    $ echo "${!a}"
    foo
    $
    

    另一个例子:

    $ x=1
    $ y=2
    $ z=3
    $ a=x
    $ echo "${!a}"
    1
    $ a=y
    $ echo "${!a}"
    2
    $ a=z
    $ echo "${!a}"
    3
    $ 
    

    还有更多:

    A variable can be assigned the nameref attribute using the -n option to the declare or local builtin commands […] to create a nameref, or a reference to another variable. This allows variables to be manipulated indirectly. Whenever the nameref variable is referenced, assigned to, unset, or has its attributes modified (other than using or changing the nameref attribute itself), the operation is actually performed on the variable specified by the nameref variable’s value.

    This way you don't need ! and you can even assign values indirectly. Example:

    $ a=t
    $ echo "$a"
    t
    $ declare -n a
    $ echo "$a"
    
    $ t=5
    $ echo "$a"
    5
    $ t=foo
    $ echo "$a"
    foo
    $ a=bar
    $ echo "$t"
    bar
    $ declare +n a
    $ echo "$a"
    t
    $
    
    • 0

相关问题

  • 在非 root 用户上用 bash 替换 zsh

  • 在 macOS High Sierra 的终端中设置环境变量时遇到问题

  • 对于 cp 或 mv,是否有等同于 cd - 的东西?

  • Notify-发送窗口下出现的通知

  • 如何从 WSL 打开 office 文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve