使用If / Then / Else语句整合Bash脚本

时间:2017-03-10 20:48:34

标签: linux bash shell

我对Bash脚本的世界有些新意,我仍然试图理解大多数人认为的基础知识。我发现this helpful guide我从中提取了大部分信息,尽管它指的是“高级”,但它实际上很好地涵盖了像我这样的新手的基础知识。

此时,我已经编写了一个可以实现我想要的脚本的脚本,但是我想我有些人可能认为是代码意识。因此,虽然我的脚本确实有效,但我认为我没有正确使用Bash的if / then / else语句。

这是我编写的小脚本,它基本上通过API查询检查用户名,以及它们是否存在。它允许用户通过./script.sh list定义列表,或者如果他们没有定义列表,则只需尝试所有字母变体({a..z}):

#!/bin/bash

# variables
host='https://api.example.com/api/users/'
sleep=1

# check if jq install
if ! jq_loc="$(type -p "jq")" || [ -z "$jq_loc" ]; then
  echo "no jq installed."
  sleep 1
  echo "we're going to install it now."
  sleep 2
  sudo apt-get install jq
fi

# specified list
if [ -n "$1" ] ; then
  list=`cat $1`
  for username in $list
  do
     #echo $username
     sleep $sleep
     VAL=`curl -s "$host$username" | jq -r ".id" | grep null`
     #echo $VAL
     if [[ $VAL == null ]]
     then
       echo -e "$username is available"
       echo "$username" >> username.free
     else
       echo -e "$username is taken"
     fi
  done
fi

# no list specified
if [ $# -eq 0 ] ; then
list=`echo {a..z}{a..z}{a..z}`
  for username in $list
  do
     #echo $username
     sleep $sleep
     VAL=`curl -s "$host$username" | jq -r ".id" | grep null`
     #echo $VAL
     if [[ $VAL == null ]]
     then
       echo -e "$username is available"
       echo "$username" >> username.free
     else
       echo -e "$username is taken"
     fi
  done
fi

正如我所说的,脚本正是我想要的。但我关注的地方是if [ $# -eq 0 ] ; thenif [ -n "$1" ] ; then。我觉得我正在呼应它不必要地做同样的功能。例如,有没有必要告诉它在每个curl中执行if命令,有没有办法可以将这两个if合并为一个?{/ p >

希望我已经对此做了很好的解释,如果没有,请注释,我可以相应地更新这个问题。

提前致谢!

2 个答案:

答案 0 :(得分:1)

您的if语句涵盖所有逻辑分支。因此,以下代码块将以任一方式执行。所以没有理由复制它。只需将其移至if语句下方即可。

for username in $list
do
   #echo $username
   sleep $sleep
   VAL=`curl -s "$host$username" | jq -r ".id" | grep null`
   #echo $VAL
   if [[ $VAL == null ]]
   then
     echo -e "$username is available"
     echo "$username" >> username.free
   else
     echo -e "$username is taken"
   fi
done

以下是为减少重复而重新编写的代码:

#!/bin/bash

# variables
host='https://api.example.com/api/users/'
sleep=1

# check if jq install
if ! jq_loc="$(type -p "jq")" || [ -z "$jq_loc" ]; then
  echo "no jq installed."
  sleep 1
  echo "we're going to install it now."
  sleep 2
  sudo apt-get install jq
fi

if [ -n "$1" ] ; then
  # specified list
  list=`cat $1`
else
  # no list specified
  list=`echo {a..z}{a..z}{a..z}`
fi

for username in $list
do
   #echo $username
   sleep $sleep
   VAL=`curl -s "$host$username" | jq -r ".id" | grep null`
   #echo $VAL
   if [[ $VAL == null ]]
   then
     echo -e "$username is available"
     echo "$username" >> username.free
   else
     echo -e "$username is taken"
   fi
done

答案 1 :(得分:0)

不是将数据读入变量然后处理该变量,而是一种常见的技术是在读取数据时处理数据。为此,我将提供以下Micah脚本的编辑:

#!/bin/bash

# variables
host='https://api.example.com/api/users/'
sleep=1

# check if jq install
type jq > /dev/null || sudo apt-get install jq

# Here is a bit of "magic".  We create a process to generate the data
# that we want to use as input and redirect its output to be the input
# of the script so that the subsequent read handles it. (Note that it
# is totally unnecessary to do this, and we could just as easily pipe
# this data directly to the while/read loop, but for some reason I 
# thought it would be instructive to see this technique.) 
exec < <(
if [ -n "$1" ] ; then
  cat $1
else
  echo {a..z}{a..z}{a..z} | tr ' ' \\n
fi
)

while read username 
do
   sleep $sleep
   if curl -s "$host$username" | jq -r ".id" | grep -q null
   then
     echo "$username is available"
     echo "$username" >> username.free
   else
     echo "$username is taken"
   fi
done
相关问题