忍者ブログ

[PR]

2024年05月04日 ()
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

fdisk: with script

2008年10月18日 (shellscript)

fdiskコマンドをスクリプトでできたらいいなと・・
まずは普通にfdisk.

command

# fdisk /dev/sdb

The number of cylinders for this disk is set to 3732.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-3732, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-3732, default 3732): +1024M

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 83

Command (m for help): a
Partition number (1-4): 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
# mkfs.ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
125184 inodes, 250032 blocks
12501 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=260046848
8 block groups
32768 blocks per group, 32768 fragments per group
15648 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
# mount -t ext3 /dev/sdb1 /mnt
# 

流れとしては
fdisk:既存のパーティションを削除→1024Mのパーティションを作成→ファイルシステムはLinux→ブート可にする→テーブルを書き込んでfdisk終了↓
mkfs.ext3:ext3でフォーマット↓
mount:ファイルシステムext3でマウント
という感じ.これをスクリプトで実行したいなと.少々分かりにくいので入力だけ抜き出す.

# fdisk /dev/sdb
> d
> n
> p
> 1
> 1
> +1024M
> t
> 83
> a
> 1
> w
# mkfs.ext3 /dev/sdb1
# mount -t ext3 /dev/sdb1 /mnt

ぐぐると案外出てきた.上記のことをスクリプトでやろうとすると・・

source

#!/bin/sh

#fdisk with shell script

USB_FLASHMEMORY=/dev/sdb

fdisk $USB_FLASHMEMORY <<\__EOF__
d
n
p
1
1
+1024M
t
83
a
1
w
__EOF__

mkfs.ext3 ${USB_FLASHMEMORY}1
mount -t ext3 ${USB_FLASHMEMORY}1 /mnt

っとまぁこんな感じになる.

PR

tag2txt.sh

2008年08月31日 (shellscript)

source

#!/bin/sh

#html tag to text in html

if [ $# -ne 1 ]; then
    echo "Usage: $0 HTML_FILE"
    exit 1
elif [ ! -r $1 ]; then
    echo "$1: permission denied"
    exit 2
fi

sed \
    -e '1,$s/\&/\&amp;/g' \
    -e '1,$s/</\&lt;/g' \
    -e '1,$s/>/\&gt;/g' \
$1

note

blogにHTMLソースを載せるときに便利かなと思う.
最初のif文は引数,ファイルが読めるかのチェック.
実際の処理部分は最後5行だけ.

sample_for2.sh

2008年08月13日 (shellscript)

source

#!/bin/sh

#sample for 2

for i in `seq 1 $1`
do
    echo $i
done

sample execution

% sh sample_for2.sh 3
1
2
3
% sh sample_for2.sh 5
1
2
3
4
5
% 

note

他言語のfor文の使いかたに近く,さらに行数を減らせるのでクールかもしれない.

rand.sh

2008年08月12日 (shellscript)

source

#!/bin/sh

#random

if [ $# -ne 1 ]; then
    echo "Usage: $0 DISIT_NUMBER"
    exit 1
fi
expr $1 + 0 >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "Usage: $0 DISIT_NUMBER"
    exit 2
fi
i=$1
while [ $i -gt 0 ] 
do
    RAND=`expr $RANDOM % 10`
    echo -n $RAND
    i=`expr $i - 1`
done
echo

sample execution

% ./rand.sh 5
73238
% ./rand.sh 10
5075273799
% ./rand.sh a
Usage: ./rand.sh DISIT_NUMBER
% ./rand.sh 5 10
Usage: ./rand.sh DISIT_NUMBER

note

引数に数字を与えたらその数字の桁数の乱数を生成するシェルスクリプト.
ASCII文字,引数が2つ以上の場合はエラーで終了.

integer or not

2008年08月08日 (shellscript)

source

#!/bin/sh

#integer or not

expr $1 + 0 >/dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "integer"
else
    echo "not integer"
fi

sample execution

% sh integerornot.sh 10
integer
% sh integerornot.sh abc
not integer
 | HOME | 次のページ »