bash で for文やwhile文を1行で書く

for i in {1..10}; do echo $i; done

while true; do echo 'hello'; done

3 Replies to “bash で for文やwhile文を1行で書く”

  1. SECRET: 0
    PASS: 74be16979710d4c4e7c6647856088456
    本文が英数字のみの場合は書き込めません。

    FILE=$1

    Lines=`wc -l < $FILE`
    CurrentLine=2
    while [ $CurrentLine -lt $Lines ]
    do
    ((CurrentLine++))
    LINE=`tail -$CurrentLine $FILE | head -1`
    j=0
    for i in $LINE
    do
    ((j++))
    if [ $j -eq 3 ]
    then
    echo -n $i" "
    fi
    done
    done
    echo

  2. SECRET: 0
    PASS: 74be16979710d4c4e7c6647856088456
    コメント投稿確認

    #!/bin/bash

    if [ $# -ne 1 -o ! -d "$1" -o ! -r "$1" ]; then
    echo ERROR
    exit 1
    fi

    NFiles=`ls -l $1 | wc -l`
    echo $NFiles

    VAR=`ls -l $1 | tr -s " " | cut -d" " -f5`
    RET=0
    for S in $VAR
    do
    ((RET = $RET + $S))
    done

    echo $RET

  3. SECRET: 0
    PASS: 74be16979710d4c4e7c6647856088456
    ※印は必須項目です。

    #!/bin/bash

    set `cat $1`
    TIMES=$1
    SEC=$2
    OUT=$3
    BAK=$4
    NUM=$5
    ERR=$6

    # flag to tell if mail message has been sent
    MAILSENT=0

    if [ -f $OUT ]
    then
    #if the maximum number of backups already exists,
    #delete the last one to make way for the next run-through
    if [ -f $OUT.$BKPNUM ]
    then
    rm $OUT.$BKPNUM
    fi

    #rename each existing backup file with the next higher numbered extension
    while [ $BAK -ge 1 ]
    do
    NEXT=`expr $BAK + 1`
    mv $OUT.$BAK $OUT.$NEXT 2>$ERR
    ((BAK–))
    done

    #rename the already existing output file with an extension of 1
    mv $OUT $OUT.1 2>>$ERR
    fi

    while [ $TIMES -gt 0 ]
    do
    USERNUM=`who | wc -l` #number of users currently logged in
    DATE=`date`
    #if number of current users >= the max set in the config file, send email
    if [ $USERNUM -ge $NUM -a $MAILSENT -eq 0 ]
    then
    `echo Number of users exceeded: $USERNUM at $DATE | mail $USER`

    echo "Max number of users met. Emailing $USER."
    MAILSENT=1 #change the variable so mail is not sent more than once
    fi
    (echo $DATE Users=$USERNUM) >> $OUT
    ((TIMES–))
    sleep $SEC
    done

Leave a Reply to Anonymous Cancel reply

Your email address will not be published. Required fields are marked *