使用“ sed”设置变量的if语句不起作用

时间:2018-09-24 13:33:53

标签: bash if-statement sed

我正在尝试获取if语句来读取文本文件(tmp.txt)的第一行,该文件的最后一行为0。然后,“ then”命令基本上进入一个目录并运行一系列用于DNA序列分析的命令,然后再进行备份,删除tmp.txt的第一行并移至tmp.txt中列出的下一个目录。一旦到达所有列出目录的末尾,最后一行将只是“ 0”或“ file-end”。问题是,它只是不起作用,我不知道为什么。我已经换掉了“ then”和“ else”命令,以使测试更加容易。

#!bin/bash/sh
value=`(sed -n 1p tmp.txt)` 
if ($value -eq 0) 
then
echo "I wish i could eat cheese again" 
else
echo "theres still more barcodes left" 
fi

1 个答案:

答案 0 :(得分:2)

>> MySQLTuner 1.7.12 - Major Hayden <major@mhtx.net> >> Bug reports, feature requests, and downloads at >> Run with '--help' for additional options and output filtering [--] Skipped version check for MySQLTuner script [OK] Logged in using credentials from Debian maintenance account. [OK] Currently running supported MySQL version 5.6.16-1~exp1 [OK] Operating on 64-bit architecture -------- Log file Recommendations ------------------------------------------------------------------ [--] Log file: /var/lib/mysql/xxx.stratoserver.net.err(0B) [!!] Log file /var/lib/mysql/xxx.stratoserver.net.err doesn't exist [!!] Log file /var/lib/mysql/xxx.stratoserver.net.err isn't readable. -------- Storage Engine Statistics ----------------------------------------------------------------- [--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM +MyISAM +PERFORMANCE_SCHEMA [--] Data in MyISAM tables: 984.2M (Tables: 30) [--] Data in InnoDB tables: 4.1G (Tables: 568) [--] Data in MEMORY tables: 1.3M (Tables: 17) [!!] Total fragmented tables: 1 -------- Analysis Performance Metrics -------------------------------------------------------------- [--] innodb_stats_on_metadata: OFF [OK] No stat updates during querying INFORMATION_SCHEMA. -------- Security Recommendations ------------------------------------------------------------------ [OK] There are no anonymous accounts for any database users [OK] All database users have passwords assigned [!!] There is no basic password file list! -------- CVE Security Recommendations -------------------------------------------------------------- [--] Skipped due to --cvefile option undefined -------- Performance Metrics ----------------------------------------------------------------------- [--] Up for: 10d 23h 42m 17s (581M q [612.461 qps], 1M conn, TX: 889G, RX: 249G) [--] Reads / Writes: 93% / 7% [--] Binary logging is disabled [--] Physical Memory : 32.0G [--] Max MySQL memory : 22.6G [--] Other process memory: 33.5M [--] Total buffers: 19.1G global + 2.8M per thread (1024 max threads) [--] P_S Max memory usage: 754M [--] Galera GCache Max memory usage: 0B [OK] Maximum reached memory usage: 20.1G (62.80% of installed RAM) [OK] Maximum possible memory usage: 22.6G (70.49% of installed RAM) [OK] Overall possible memory usage with other process is compatible with memory available [OK] Slow queries: 0% (365K/581M) [OK] Highest usage of available connections: 10% (108/1024) [OK] Aborted connections: 0.02% (338/1784303) [!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance [!!] Query cache may be disabled by default due to mutex contention. [!!] Query cache efficiency: 0.0% (0 cached / 459M selects) [OK] Query cache prunes per day: 0 [OK] Sorts requiring temporary tables: 0% (1K temp sorts / 92M sorts) [!!] Joins performed without indexes: 341633 [!!] Temporary tables created on disk: 47% (32M on disk / 69M total) [OK] Thread cache hit rate: 99% (108 created / 1M connections) [!!] Table cache hit rate: 2% (2K open / 98K opened) [OK] Open file limit used: 0% (132/25K) [OK] Table locks acquired immediately: 99% (1B immediate / 1B locks) -------- Performance schema ------------------------------------------------------------------------ [--] Memory used by P_S: 754.8M [--] Sys schema isn't installed. -------- ThreadPool Metrics ------------------------------------------------------------------------ [--] ThreadPool stat is disabled. -------- MyISAM Metrics ---------------------------------------------------------------------------- [!!] Key buffer used: 52.9% (284M used / 536M cache) [OK] Key buffer size / total MyISAM indexes: 512.0M/378.4M [OK] Read Key buffer hit rate: 100.0% (484M cached / 171K reads) [!!] Write Key buffer hit rate: 76.0% (42M cached / 32M writes) -------- InnoDB Metrics ---------------------------------------------------------------------------- [--] InnoDB is enabled. [--] InnoDB Thread Concurrency: 0 [OK] InnoDB File per table is activated [OK] InnoDB buffer pool / data size: 16.0G/4.1G [!!] Ratio InnoDB log file size / InnoDB Buffer pool size (6.25 %): 512.0M * 2/16.0G should be equal 25% [!!] InnoDB buffer pool instances: 8 [--] InnoDB Buffer Pool Chunk Size not used or defined in your version [OK] InnoDB Read buffer efficiency: 100.00% (238662528177 hits/ 238662686106 total) [!!] InnoDB Write Log efficiency: 88.34% (208711294 hits/ 236256359 total) [OK] InnoDB log waits: 0.00% (0 waits / 27545065 writes) -------- AriaDB Metrics ---------------------------------------------------------------------------- [--] AriaDB is disabled. -------- TokuDB Metrics ---------------------------------------------------------------------------- [--] TokuDB is disabled. -------- XtraDB Metrics ---------------------------------------------------------------------------- [--] XtraDB is disabled. -------- Galera Metrics ---------------------------------------------------------------------------- [--] Galera is disabled. -------- Replication Metrics ----------------------------------------------------------------------- [--] Galera Synchronous replication: NO [--] No replication slave(s) for this server. [--] Binlog format: STATEMENT [--] XA support enabled: ON [--] Semi synchronous replication Master: Not Activated [--] Semi synchronous replication Slave: Not Activated [--] This is a standalone server -------- Recommendations --------------------------------------------------------------------------- General recommendations: Run OPTIMIZE TABLE to defragment tables for better performance OPTIMIZE TABLE `juwelierwebshop`.`cron_schedule`; -- can free 1064 MB Total freed space after theses OPTIMIZE TABLE : 1064 Mb Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1 Adjust your join queries to always utilize indexes When making adjustments, make tmp_table_size/max_heap_table_size equal Reduce your SELECT DISTINCT queries which have no LIMIT clause Increase table_open_cache gradually to avoid file descriptor limits Read this before increasing table_open_cache over 64: This is MyISAM only table_cache scalability problem, InnoDB not affected. See more details here: This bug already fixed in MySQL 5.7.9 and newer MySQL versions. Beware that open_files_limit (25610) variable should be greater than table_open_cache (12288) Consider installing Sys schema from Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: Variables to adjust: query_cache_size (=0) query_cache_type (=0) query_cache_limit (> 4M, or use smaller result sets) join_buffer_size (> 128.0K, or always use indexes with JOINs) tmp_table_size (> 64M) max_heap_table_size (> 512M) table_open_cache (> 12288) innodb_log_file_size should be (=2G) if possible, so InnoDB total log files size equals to 25% of buffer pool size. innodb_buffer_pool_instances(=16) 中那样的假体创建子外壳来运行附带的命令。bash命令根本不需要它们,它们也不是您想要的sed命令中的测试值。如今,每天运行命令并存储其值的首选方法是使用if语法,而不是使用反引号。

对于算术测试,您可以使用双括号$(...),也可以为((...))使用[同义词,或者test的扩展版本为bash。特别是对于[[...]]来说,括号后的空格是必不可少的,因为在这种情况下,您尝试运行命令[

将这些内容放在一起,我们可以像这样更新您的代码段:

[

(此外,我将shebang行固定为指向#!/bin/bash value=$(sed -n 1p tmp.txt) if [[ $value -eq 0 ]] then echo "I wish i could eat cheese again" else echo "theres still more barcodes left" fi ,而不是一个显然不存在的名为/bin/bash的可执行文件)