bash

Bash Boolean testing

僤鯓⒐⒋嵵緔 提交于 2021-02-05 20:11:37
问题 I am attempting to run a block of code if one flag is set to true and the other is set to false. ie var1=true var2=false if [[ $var1 && ! $var2 ]]; then var2="something"; fi Since that did not evaluate the way that I expected I wrote several other test cases and I am having a hard time understanding how they are being evaluated. aa=true bb=false cc="python" if [[ "$aa" ]]; then echo "Test0" ; fi if [[ "$bb" ]]; then echo "Test0.1" ; fi if [[ !"$aa" ]]; then echo "Test0.2" ; fi if [[ ! "$aa" ]

关于shell编程需要记忆(基础)

拈花ヽ惹草 提交于 2021-02-05 15:27:05
关于shell编程需要记忆(基础) 写完shell脚本要用chmod命令加上x(执行)权限 一、shell基础 一、通配符 文件名的扩展成为通配,通配定义了一套使用特殊字符的规则。当输入文件名作为参数的命令时, 可以使用特定的通配符去匹配多个文件 表1-1-1 常用的通配符 符号 含义 * 匹配任意字符的0次或多次出现 ? 匹配任意零个或单个字符 [ ] 匹配字符串中所限定的任何一个字符 [^ ]或[! ] 表示反向选择,匹配不在该字符串的任意字符 {string1,string2,…} 匹配其中一个指定的字符串 1.*(星号) eg:匹配所有以".png"结尾的,星号可放在任意位置,匹配任意一段字符。 ls *.png 2.?(问号) ?匹配任意单个字符 ?*y匹配以任意单个字符开头以*y结尾的文件名 总而言之: ?与*号不同,?只能匹配 一个字符 3.[](一对方括号) [ .]中无论有多少个字符,都只代表某一个字符。例如:[hfchg] 匹配以 *[ ]中**任意一个字符开头,以任意内容结尾的。 "-"可以指定范围例如:[a-z]匹配a-z之间任意一个字符。 4.[^]或者 [!] 表示反向选择 例如:[^Aa]* 表示匹配不以A或者a开头的任意文件名。 5.{} 匹配其中一个指定的字符串, 例如:{string1,string2} 表示依次以string1

How do I write non-ASCII characters using echo?

早过忘川 提交于 2021-02-05 14:44:30
问题 How do I write non-ASCII characters using echo? Is there an escape sequence, such as \012 or something like that? I want to append ASCII characters to a file using: echo ?? >> file 回答1: Use echo -e "\012" 回答2: If you care about portability, you'll drop echo and use printf(1) : printf '\012' 回答3: On my terminal, printf '\012' >>output.txt works for both the octal representation of the ascii character, and the corresponding hexadecimal: printf '\xA' >>output.txt The command echo -en '\012' >

How do I write non-ASCII characters using echo?

北城以北 提交于 2021-02-05 14:44:08
问题 How do I write non-ASCII characters using echo? Is there an escape sequence, such as \012 or something like that? I want to append ASCII characters to a file using: echo ?? >> file 回答1: Use echo -e "\012" 回答2: If you care about portability, you'll drop echo and use printf(1) : printf '\012' 回答3: On my terminal, printf '\012' >>output.txt works for both the octal representation of the ascii character, and the corresponding hexadecimal: printf '\xA' >>output.txt The command echo -en '\012' >

How do I write non-ASCII characters using echo?

末鹿安然 提交于 2021-02-05 14:43:58
问题 How do I write non-ASCII characters using echo? Is there an escape sequence, such as \012 or something like that? I want to append ASCII characters to a file using: echo ?? >> file 回答1: Use echo -e "\012" 回答2: If you care about portability, you'll drop echo and use printf(1) : printf '\012' 回答3: On my terminal, printf '\012' >>output.txt works for both the octal representation of the ascii character, and the corresponding hexadecimal: printf '\xA' >>output.txt The command echo -en '\012' >

jenkins执行shell命令修改不了目录的问题

前提是你 提交于 2021-02-05 14:43:26
jenkins默认是有一个当前目录作为运行环境,执行的操作均基于此目录,如果创建的jenkins项目是free style的,如果要执行远程命令,就不方便修改执行目录了。 例子:如果想通过jenkins执行一个sh脚本,里面有cd命令,项目是free style的,则用exectue command over ssh,会出一build不结束的现象。原因就是sh里不能出现cd命令去修改当前执行的目录。 要如何解决这个问题呢? 1种可选的方案是使用pipeline项目,用dir(目录){}代码来限制。但是pipeline使用上稍复杂,这里只是简单的执行一个命令,有点不太必要。 2种方案是使用嵌套sh调用。即jenkins执行一个简单的脚本,这个脚本再调用真正执行的脚本。其本质上是重新执行一个后台任务。如 jenkins.sh #/bin/bash nohup /dir/another.sh >/dev/null 2>&1 exit 0 another.sh才是真正的脚本,里面可以使用任何命令了。 来源: oschina 链接: https://my.oschina.net/swingcoder/blog/4946527

Grep and set -o errexit

坚强是说给别人听的谎言 提交于 2021-02-05 12:32:34
问题 I have a simple bash-script. It finds a string in the file. #/bin/bash set -o errexit grep 'findedstring' $file. echo "was founded string on file" <...> If the string was found in the file the script was successfully executed and I can see "was founded string on file" in the output . But if the file doesn't have 'findedstring' then the script exits and didn't work further, so I don't see "was founded string on file" in the output If I will try to delete the following string in my script 'set

no output from unix shell script

十年热恋 提交于 2021-02-05 12:26:24
问题 I have the following shell script (named test ): #!/bin/sh echo "junk" filename="junk" echo $filename filename=`ls -t|head -n1` echo $filename when I run this script there is no output to the terminal. I am using red hat / putty. What am I missing? 回答1: You're accidentally running the system command called test , which produces no output. You need to use ./test instead to find the script in the current directory, whereas test will use the built-in shell command (and even if it didn't, it

no output from unix shell script

我的未来我决定 提交于 2021-02-05 12:26:05
问题 I have the following shell script (named test ): #!/bin/sh echo "junk" filename="junk" echo $filename filename=`ls -t|head -n1` echo $filename when I run this script there is no output to the terminal. I am using red hat / putty. What am I missing? 回答1: You're accidentally running the system command called test , which produces no output. You need to use ./test instead to find the script in the current directory, whereas test will use the built-in shell command (and even if it didn't, it

Re asking for a variable after execution in bash (like an alternative to goto) [closed]

僤鯓⒐⒋嵵緔 提交于 2021-02-05 12:10:36
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 days ago . Improve this question I'm reformulating a bit better my previous question from today. So basically, I have a bash script where i have variable=$(zenity list dialog) if test $variable = "something i've checked in zenity" then run command & restart the script elif... ...another command, etc... fi